1

我正在开发一个“Apache Oltu Spring MVC Github”集成示例。在这个例子中,我将发送“App ID”和“Secret”来获取“access_token”,以便访问“Gist”、“user”等受保护的资源。

所以第一步是使用https://github.com/settings/applications/new创建/注册“应用程序” 。创建应用程序后,请务必注意:AppID 和 Secret,我们需要在 Spring 代码中使用这些值。

要开发此功能/代码 - 我搜索了很多,但没有找到任何现成的代码。所以我决定在下面提供/解释我的代码。因此,人们会发现这些链接很有用。

我参考了以下 URL 来开发整个代码:-

4

2 回答 2

1

附件是在 Github 上注册“应用程序”的屏幕截图。“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

运行代码时得到的输出:

在此处输入图像描述

于 2015-05-02T19:15:33.217 回答
1

user4798111 提到的是正确的,只是添加了更多细节。先决条件,您需要在 Github 上注册 App。注册应用程序后,您将获得CLIENT_SECRET,CLIENT_ID用于从 github 获取受保护的资源。

如果您使用OAuthClientRequestAPI 进行初始调用,您需要具备以下详细信息

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
   }
}
于 2017-09-26T21:34:28.220 回答