user4798111 提到的是正确的,只是添加了更多细节。先决条件,您需要在 Github 上注册 App。注册应用程序后,您将获得CLIENT_SECRET
,CLIENT_ID
用于从 github 获取受保护的资源。
如果您使用OAuthClientRequest
API 进行初始调用,您需要具备以下详细信息
private static final String AUTHORIZATION_URL = "https://github.com/login/oauth/authorize";
private static final String CLIENT_ID = "8515a1e4XXXXXXX";
private static final String CLIENT_SECRET = "ae3487601d891d257f7193XXXXXXXXX";
private static final String REDIRECT_URL = "http://localhost:8080/apache-oltu/github/redirect";
private static final String ACCESS_TOKEN_URL = "https://github.com/login/oauth/access_token";
@RequestMapping(value = "/auth", method = RequestMethod.GET)
public String authenticate() throws OAuthSystemException {
OAuthClientRequest request = OAuthClientRequest
.authorizationLocation(AUTHORIZATION_URL)
.setClientId(CLIENT_ID)
.setRedirectURI(REDIRECT_URL)
.setResponseType("code")
.setScope("user,gist,user:email,user:follow,public_repo,repo,repo_deployment,repo:status,repo:invite")
.buildQueryMessage();
System.out.println("REDIRECT TO: "+request.getLocationUri());
return "redirect:" + request.getLocationUri();
}
您需要使用相同的东西,如下所示
request= new OAuthBearerClientRequest("https://api.github.com/user").
setAccessToken(oAuthResponse.getAccessToken()).
buildQueryMessage();
可以在此处找到有关范围和其他详细信息的信息:
可以看到的结果如下供参考:
{
"login":"JavaHelper",
"id":8208031,
"avatar_url":"https://avatars0.githubusercontent.com/u/8208031?v=4",
"gravatar_id":"",
"url":"https://api.github.com/users/JavaHelper",
"html_url":"https://github.com/JavaHelper",
"followers_url":"https://api.github.com/users/JavaHelper/followers",
"following_url":"https://api.github.com/users/JavaHelper/following{/other_user}",
"gists_url":"https://api.github.com/users/JavaHelper/gists{/gist_id}",
"starred_url":"https://api.github.com/users/JavaHelper/starred{/owner}{/repo}",
"subscriptions_url":"https://api.github.com/users/JavaHelper/subscriptions",
"organizations_url":"https://api.github.com/users/JavaHelper/orgs",
"repos_url":"https://api.github.com/users/JavaHelper/repos",
"events_url":"https://api.github.com/users/JavaHelper/events{/privacy}",
"received_events_url":"https://api.github.com/users/JavaHelper/received_events",
"type":"User",
"site_admin":false,
"name":"JavaProgramer",
"company":null,
"blog":"",
"location":null,
"email":null,
"hireable":null,
"bio":null,
"public_repos":45,
"public_gists":0,
"followers":4,
"following":60,
"created_at":"2014-07-19T10:03:42Z",
"updated_at":"2017-09-09T12:55:57Z",
"private_gists":0,
"total_private_repos":0,
"owned_private_repos":0,
"disk_usage":142270,
"collaborators":0,
"two_factor_authentication":false,
"plan":{
"name":"free",
"space":976562499,
"collaborators":0,
"private_repos":0
}
}