Apache Oltu Github 与 Spring MVC 的集成示例
Apache Oltu Github integration example with Spring MVC
我正在开发一个 "Apache Oltu Spring MVC Github" 集成示例。在此示例中,我将发送 "App ID" 和 "Secret" 以获取 "access_token" 以访问受保护的资源,如 "Gist"、"user" 等
所以第一步是使用 https://github.com/settings/applications/new 创建/注册 "App"。
创建应用程序后,请务必注意:AppID 和 Secret,我们需要在 Spring 代码中使用这些值。
为了开发这个功能/代码——我搜索了很多,但没有找到任何现成的代码。所以我决定在下面提供/解释我的代码。因此,您会发现这些链接很有用。
我参考了以下 URL 来开发整个代码:-
附件是在 Github 上注册 "App" 的屏幕截图。 "MyApp" 是我创建的应用程序。
使用 http://www.jasha.eu/blogposts/2013/09/retrieve-facebook-profile-data-java-apache-oltu.html 中的相同代码,只需确保更改
AUTHORIZATION_URL = "https://github.com/login/oauth/authorize";
ACCESS_TOKEN_URL = "https://github.com/login/oauth/access_token"
要获取用户配置文件等受保护资源,请使用:https://api.github.com/user
我在 运行 代码时得到的输出:
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
}
}
我正在开发一个 "Apache Oltu Spring MVC Github" 集成示例。在此示例中,我将发送 "App ID" 和 "Secret" 以获取 "access_token" 以访问受保护的资源,如 "Gist"、"user" 等
所以第一步是使用 https://github.com/settings/applications/new 创建/注册 "App"。 创建应用程序后,请务必注意:AppID 和 Secret,我们需要在 Spring 代码中使用这些值。
为了开发这个功能/代码——我搜索了很多,但没有找到任何现成的代码。所以我决定在下面提供/解释我的代码。因此,您会发现这些链接很有用。
我参考了以下 URL 来开发整个代码:-
附件是在 Github 上注册 "App" 的屏幕截图。 "MyApp" 是我创建的应用程序。
使用 http://www.jasha.eu/blogposts/2013/09/retrieve-facebook-profile-data-java-apache-oltu.html 中的相同代码,只需确保更改
AUTHORIZATION_URL = "https://github.com/login/oauth/authorize";
ACCESS_TOKEN_URL = "https://github.com/login/oauth/access_token"
要获取用户配置文件等受保护资源,请使用:https://api.github.com/user
我在 运行 代码时得到的输出:
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
}
}