在谷歌的OAuth2文档中,使用身份验证令牌构建GoogleCredential
如下所示:
Credential and Credential Store
特别地,提供了以下代码片段:
GoogleCredential credential = new GoogleCredential().setAccessToken(accessToken);
Plus plus = Plus.builder(new NetHttpTransport(), new JacksonFactory())
.setApplicationName("Google-PlusSample/1.0")
.setHttpRequestInitializer(credential)
.build()
当我尝试以这种方式构建一个GoogleCredential
时,我得到的信息非常简洁:
Please use the Builder and call setJsonFactory, setTransport and setClientSecrets
在异常的消息字段中。我上周下载了这个库,所以我不确定发生了什么。文档是不是已经过时了?如果是的话,是什么方法取代了这个方法,成为从现有的身份验证令牌和刷新令牌构建的最佳实践?
顺便说一句,使用Builder不是一个选择,因为Google应用程序控制台没有提供客户端机密;它说它们不再为Android应用程序和类似应用程序提供。因此,无法调用setClientSecrets(...)
。
发布于 2014-08-16 04:54:04
我最近遇到了这个问题,并为我的案例找到了解决方案。
以下是运行条件:程序在Android 4.0上运行,不使用Google Drive SDK,因为它不允许我们的程序读取Google Drive上的文件,这些文件不是由我们的程序创建的。我使用com.google.api.services.drive.*和com.google.api.client.googleapis.auth.oauth2.* Java库。
引发此问题的代码如下:
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET, Arrays.asList(DriveScopes.DRIVE))
.setAccessType("offline")
.setApprovalPrompt("auto").build();
GoogleTokenResponse response = null;
try {
response = flow.newTokenRequest(authorizationCode).setRedirectUri(REDIRECT_URI).execute();
} catch (IOException e) {
e.printStackTrace();
}
GoogleCredential credential = new GoogleCredential().setFromTokenResponse(response);
但如果将参数设置为setAccessType(“online”),则上述代码运行良好;
这个问题的解决方案取决于你想要什么样的accessType。
如果您想要"onLine“作为accessType,那么使用setFromTokenResponse()应该是可以的。
如果你想要“离线”作为accessType,那么你需要使用blow代码:
GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setClientSecrets(CLIENT_ID, CLIENT_SECRET)
.build()
.setFromTokenResponse(response);
需要提到的一件事是,对于GoogleTokenResponse和GoogleAuthorizationCodeFlow创建,您需要保持accessType设置一致。
发布于 2014-11-21 16:54:46
你需要在Google Dev Console中设置你的安卓应用密钥。
选择您的项目,选择API & Auth,然后单击Credentials
<>H114像这样填写SHA1;packageName:
您的问题将自动解决。确保使用您的调试密钥库和发布密钥库创建客户端id和密钥。
发布于 2013-02-26 02:42:29
查看Google Drive SDK文档中的Android快速入门示例,获得正确设置GoogleCredential
的分步说明
https://stackoverflow.com/questions/15064636
复制相似问题