其中我得到错误:
com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
{
"code" : 401,
"errors" : [ {
"domain" : "global",
"location" : "Authorization",
"locationType" : "header",
"message" : "Invalid Credentials",
"reason" : "authError"
} ],
"message" : "Invalid Credentials"
}
下面的代码,我使用:
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(this.TRANSPORT).setJsonFactory(this.JSON_FACTORY)
.setClientSecrets(Constants.CLIENT_ID, Constants.CLIENT_SECRET).build();
credential.setAccessToken(tokenResponse.getAccessToken());
credential.setAccessToken(tokenResponse.getRefreshToken());
到这里,我得到刷新令牌,访问令牌等
Oauth2 userInfoService = new Oauth2.Builder(this.TRANSPORT,
this.JSON_FACTORY, credential.getRequestInitializer())
.setApplicationName(Constants.APPLICATION_NAME).build();
它无法以低于行:(不知道,为什么呢?)
Userinfo userInfo = userInfoService.userinfo().get().execute();
我搜索的网页,和我得到的非常少的例子和稀有材料。 任何身体上有什么想法?
我究竟做错了什么?
我猜credential.getRequestInitializer()为null。
我由一个自定义的请求初始化设置这样的凭证对象解决了这个
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(this.TRANSPORT).setJsonFactory(this.JSON_FACTORY)
.setClientSecrets(Constants.CLIENT_ID, Constants.CLIENT_SECRET).setRequestInitializer((new HttpRequestInitializer(){
@Override
public void initialize(HttpRequest request)
throws IOException {
request.getHeaders().put("Authorization", "Bearer " + accessToken);
}
})).build()
谷歌的文档 especifies如下:
**例如,使用查询的access_token字符串参数到用户信息API的调用如下所示:
GET https://www.googleapis.com/oauth2/v1/userinfo?access_token= {的accessToken}使用访问令牌在HTTP头看起来如下到同一API的调用:
GET /的oauth2 / V1 / userinfo的HTTP / 1.1授权:承载{的accessToken}主机:googleapis.com **
希望对你有帮助
如果您已经获得访问令牌( GoogleTokenResponse ),那么你也可以这样做:
HttpTransport transport = new NetHttpTransport();
List<String> applicationScopes = Arrays.asList(
PlusScopes.USERINFO_EMAIL,
PlusScopes.USERINFO_PROFILE
);
GoogleAuthorizationCodeFlow flow
= new GoogleAuthorizationCodeFlow.Builder(
transport,
JacksonFactory.getDefaultInstance(),
"your-client-id.apps.googleusercontent.com",
"your-client-secret",
applicationScopes).build();
String userId = googleTokenResponse.parseIdToken().getPayload().getSubject();
Credential credential = flow.createAndStoreCredential(googleTokenResponse, userId);
HttpRequestFactory requestFactory = transport.createRequestFactory(credential);
GenericUrl url = new GenericUrl("https://www.googleapis.com/oauth2/v1/userinfo");
HttpRequest request = requestFactory.buildGetRequest(url);
String userIdentity = request.execute().parseAsString();
该userIdentity
则是这样的:
{
"id": "105358994046791627189",
"name": "Benny Neugebauer",
"given_name": "Benny",
"family_name": "Neugebauer",
"link": "https://plus.google.com/+BennyNeugebauer",
"picture": "https://lh4.googleusercontent.com/-dtvDIXCEtFc/AAAAAAAAAAI/AAAAAAAAAoE/1CKd3nH9rRo/photo.jpg",
"gender": "male",
"locale": "de"
}
如果你愿意,你可以解析userIdentity
使用杰克逊到自己的类:
ObjectMapper mapper = new org.codehaus.jackson.map.ObjectMapper();
mapper.readValue(userIdentity, YourUser.class);
下面是我用这个例子的依赖关系:
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-plus</artifactId>
<version>v1-rev401-1.22.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
<type>jar</type>
</dependency>
为了从您有权要求其OAuth的范围访问的用户信息API检索数据:
https://www.googleapis.com/auth/userinfo.profile
此外,添加范围https://www.googleapis.com/auth/userinfo.email
如果你想获取电子邮件地址。
在你的代码,我没有看到你在哪里设置您请求访问的OAuth范围。