作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
HttpTransport netTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
GoogleTokenResponse token;
try {
token = new GoogleAuthorizationCodeTokenRequest(
netTransport,
jsonFactory,
CLIENT_ID,
CLIENT_SECRET,
CODE,
REDIRECT).execute();
GoogleCredential cd = new GoogleCredential().setAccessToken(token
.getAccessToken());
Plus plus = Plus
.builder(new NetHttpTransport(), new JacksonFactory())
.setApplicationName(APP_NAME)
.setHttpRequestInitializer(cd).build();
Person profile = plus.people().get("me").execute();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
大家好,我正在尝试在我的 Google App Engine 应用程序中从 Google+ 获取一些用户信息,但我对放置什么/如何获取 GoogleAuthorizationCodeTokenRequest 的 CODE 参数感到困惑。任何帮助是极大的赞赏。谢谢。
最佳答案
我会尝试改进 Remy Ticona 的回答。基本的 oAuth 授权流程是:
urn:ietf:wg:oauth:2.0:oob:auto
作为 url。当用户授予访问权限时,您的应用应从网页标题中读取访问代码。urn:ietf:wg:oauth:2.0:oob
作为 REDIRECT_URL。当用户授予访问权限时,他将被重定向到包含访问代码的页面,并请求将其复制并粘贴到您的应用中。您可以在这里找到详细信息:https://developers.google.com/identity/protocols/OAuth2InstalledApp?hl=RU#choosingredirecturi
小例子:
GoogleAuthorizationCodeFlow authorizationCodeFlow = new GoogleAuthorizationCodeFlow
.Builder(httpTransport, jsonFactory, clientId, clientSecret, scopes)
.setCredentialDataStore(new MemoryDataStoreFactory().getDataStore("tokens"))
.build();
String userId = "user-id";
Credential credential = authorizationCodeFlow.loadCredential(userId);
if (credential == null) {
GoogleAuthorizationCodeRequestUrl authorizationUrl = authorizationCodeFlow.newAuthorizationUrl();
authorizationUrl.setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI);
LOGGER.error("Please, authorize application. Visit {}", authorizationUrl);
Scanner s = new Scanner(System.in);
String code = s.nextLine();
GoogleAuthorizationCodeTokenRequest tokenRequest = authorizationCodeFlow.newTokenRequest(code);
tokenRequest.setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI);
GoogleTokenResponse tokenResponse = tokenRequest.execute();
credential = authorizationCodeFlow.createAndStoreCredential(tokenResponse, userId);
}
关于java - 如何在 Java 中获取 GoogleAuthorizationCodeTokenRequest 的授权码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13450460/
HttpTransport netTransport = new NetHttpTransport(); JsonFactory jsonFactory = new JacksonFa
我是一名优秀的程序员,十分优秀!