使用Spring Social通过OAuth2令牌检索GitHub用户ID

时间:2013-12-23 13:51:18

标签: java spring oauth-2.0 github-api

假设我已完成OAuth2登录并拥有身份验证令牌。

我正在尝试使用该令牌获取用户名。这就是我正在做的事情:

final GitHub gitHub = connectionFactory.createConnection(new AccessGrant(authToken)).getApi();
final String userId = gitHub.userOperations().getProfileId();

这是我得到的gitHub.userOperations().getProfileId()

org.springframework.web.client.HttpClientErrorException: 401 Unauthorized
org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:88)
org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:537)
org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:493)
org.springframework.web.client.RestTemplate.execute(RestTemplate.java:452)
org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:222)
org.springframework.social.github.api.impl.UserTemplate.getUserProfile(UserTemplate.java:50)
org.springframework.social.github.connect.GitHubAdapter.setConnectionValues(GitHubAdapter.java:43)
org.springframework.social.github.connect.GitHubAdapter.setConnectionValues(GitHubAdapter.java:30)
org.springframework.social.connect.support.AbstractConnection.setValues(AbstractConnection.java:172)
org.springframework.social.connect.support.AbstractConnection.initKey(AbstractConnection.java:135)
org.springframework.social.connect.support.OAuth2Connection.<init>(OAuth2Connection.java:73)
org.springframework.social.connect.support.OAuth2ConnectionFactory.createConnection(OAuth2ConnectionFactory.java:91)
net.librarian.app.service.impl.OAuth2Service.getAuthenticatedUserName(OAuth2Service.java:36)

所以有人知道我是否误解了某个地方的API?根据我的理解,如果我有auth令牌(user范围),我应该能够获得用户信息吗?

谢谢,


回答评论。这个curl -i https://api.github.com/user?access_token=TOKEN让我:

HTTP/1.1 401 Unauthorized
Server: GitHub.com
Date: Wed, 25 Dec 2013 13:41:42 GMT
Content-Type: application/json; charset=utf-8
Status: 401 Unauthorized
X-GitHub-Media-Type: github.beta
X-Content-Type-Options: nosniff
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 59
X-RateLimit-Reset: 1387982502
Content-Length: 92
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: ETag, Link, X-RateLimit-Limit, X-RateLimit-Remaining, X-        RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interva
Access-Control-Allow-Origin: *
X-GitHub-Request-Id: BCFEC363:32CA:5A1392E:52BAE095

{
  "message": "Bad credentials",
  "documentation_url": "http://developer.github.com/v3"
}

在我请求“新鲜”令牌后,我发出curl请求的时间不超过一分钟(实际上大约10秒)。

0 个答案:

没有答案