Oauth ApacheOltuGithub与SpringMVC的集成示例

Oauth ApacheOltuGithub与SpringMVC的集成示例,oauth,oauth-2.0,spring-security-oauth2,oltu,Oauth,Oauth 2.0,Spring Security Oauth2,Oltu,我正在开发一个“ApacheOLTUSpringMVCgithub”集成示例。在本例中,我将发送“appid”和“Secret”以获取“access_token”,以便访问受保护的资源,如“Gist”、“user”等 因此,第一步是使用创建/注册“应用程序”。 创建应用程序后,请务必注意:AppID和Secret,我们需要在Spring代码中使用这些值 为了开发这个功能/代码-我搜索了很多,但没有找到任何现成的代码。所以我决定在下面提供/解释我的代码。所以你会发现这些链接很有用 我参考了以下UR

我正在开发一个“ApacheOLTUSpringMVCgithub”集成示例。在本例中,我将发送“appid”和“Secret”以获取“access_token”,以便访问受保护的资源,如“Gist”、“user”等

因此,第一步是使用创建/注册“应用程序”。 创建应用程序后,请务必注意:AppID和Secret,我们需要在Spring代码中使用这些值

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

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


附件是在Github上注册“应用程序”的屏幕截图。“MyApp”是我创建的应用程序

使用来自的相同代码,只需确保更改

授权_URL=“”

访问令牌URL=“”

要获取受保护的资源(如用户配置文件),请使用:

运行代码时得到的输出:


用户4798111提到的内容是正确的,只是添加了更多细节。前提条件是,您需要在Github上注册应用程序。注册应用程序后,您将获得
CLIENT\u SECRET
CLIENT\u ID
,用于从github获取受保护的资源

如果使用
oauthclienterquest
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
   }
}