2

谁能告诉我如何使用 OAuth 2.0 从 googleplus 获取访问令牌用于 Web 服务器应用程序。如果您有任何代码,请在此处粘贴,将不胜感激

4

1 回答 1

3

以下是获取访问令牌的方法,但在此之前您需要获取授权码

//获取授权码的代码

GoogleAuthorizationCodeRequestUrl authUrl = new GoogleAuthorizationCodeRequestUrl(
        CLIENT_ID, REDIRECT_URI,scopes);
         authUrl.setAccessType("offline");
    authUrl.setApprovalPrompt("force");

//获取访问令牌的代码

 HttpTransport netTransport = new NetHttpTransport();
      JsonFactory jsonFactory = new JacksonFactory();
      token = new GoogleAuthorizationCodeTokenRequest(netTransport, 
        jsonFactory, CLIENT_ID, CLIENT_SECRET, authorizationCode, 
        REDIRECT_URI).execute();
      System.out.println("Valid access token " + token.getAccessToken());
       GoogleCredential cd = new GoogleCredential().setAccessToken(token
        .getAccessToken());

此代码使用

google-api-client 1.9.0-beta google-api-services-plus v1-rev11-1.6.0-beta

于 2012-06-11T18:53:28.910 回答