简体   繁体   English

Google Oauth2-令牌调用

[英]Google Oauth2 - Call to Token

I am reading though the Google API and it says to: 我正在阅读Google API,它说:

"The first step is to call loadCredential(String) based on the known user ID to check if the end-user's credentials are already known. If not, call newAuthorizationUrl() and direct the end-user's browser to an authorization page. The web browser will then redirect to the redirect URL with a "code" query parameter which can then be used to request an access token using newTokenRequest(String). Finally, use createAndStoreCredential(TokenResponse, String) to store and obtain a credential for accessing protected resources." “第一步是根据已知的用户ID调用loadCredential(String)来检查最终用户的凭据是否已知。否则,请调用newAuthorizationUrl()并将最终用户的浏览器定向到授权页面。Web然后,浏览器将使用“代码”查询参数重定向到重定向URL,然后可以使用newTokenRequest(String)请求该令牌来访问访问令牌。最后,使用createAndStoreCredential(TokenResponse,String)存储和获取访问受保护资源的凭据”。

So I place the line of code in my code, and it fails even with proper import statements. 因此,我将代码行放入我的代码中,即使使用正确的导入语句,它也会失败。 "Type mismatch: cannot convert from Credential to AuthorizationCodeRequestUrl" “类型不匹配:无法从凭据转换为AuthorizationCodeRequestUrl”

AuthorizationCodeRequestUrl bob2 = AuthorizationCodeFlow.loadCredential("test"); or
GoogleAuthorizationCodeFlow bob2 = AuthorizationCodeFlow.loadCredential("test");

http://javadoc.google-oauth-java-client.googlecode.com/hg/1.7.0-beta/com/google/api/client/auth/oauth2/AuthorizationCodeFlow.html?is-external=true#loadCredential%28java.lang.String%29 http://javadoc.google-oauth-java-client.googlecode.com/hg/1.7.0-beta/com/google/api/client/auth/oauth2/AuthorizationCodeFlow.html?is-external=true#loadCredential% 28java.lang.String%29

Also, if anyone has any good example for Oauth2 implementations for Google, please post them. 另外,如果有人对Google的Oauth2实现有很好的例子,请发布它们。 The only one I can find that I can understand (see link below) seems depreciated, and I want to use the latest version. 我发现我能理解的唯一一个(参见下面的链接)似乎已过时,并且我想使用最新版本。 https://developers.google.com/google-apps/tasks/oauth-authorization-callback-handler https://developers.google.com/google-apps/tasks/oauth-authorization-callback-handler

sHere is the code I used. 这是我使用的代码。 It is not a complete example, but it should give someone the idea. 这不是一个完整的示例,但是应该可以使某人知道。

UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();
   // response.getWriter().println(" User " + user);
    if (user == null) {
        response.sendRedirect(userService.createLoginURL(getFullRequestUrl(request)));


        return;
      }

Then I get the authorization code no matter what. 然后无论如何我都会获得授权码。 (Bad coding.) (错误编码。)

response.sendRedirect(new GoogleAuthorizationRequestUrl(oauthProperties2.getClientId(), OAuthCodeCallbackHandlerServlet.getOAuthCodeCallbackHandlerUrl(request), oauthProperties2.getScopesAsString()).build());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM