我正在尝试访问 github api( https://api.github.com/user ),如抄写员库示例中所述 ( https://github.com/scribejava/scribejava/blob/master/scribejava-apis/src/test/java/com/github/scribejava/apis/examples/GitHubExample.java )
返回给我这个授权网址
但现在我必须提供上面链接示例中提到的授权代码
final Scanner in = new Scanner(System.in, "UTF-8");
System.out.println("=== " + NETWORK_NAME + "'s OAuth Workflow ===");
System.out.println();
// Obtain the Authorization URL
System.out.println("Fetching the Authorization URL...");
final String authorizationUrl = service.getAuthorizationUrl();
System.out.println("Got the Authorization URL!");
System.out.println("Now go and authorize ScribeJava here:");
System.out.println(authorizationUrl);
System.out.println("And paste the authorization code here");
System.out.print(">>");
final String code = in.nextLine();
System.out.println();
System.out.println("And paste the state from server here. We have set 'secretState'='" + secretState + "'.");
System.out.print(">>");
final String value = in.nextLine();
if (secretState.equals(value)) {
System.out.println("State value does match!");
} else {
System.out.println("Ooops, state value does not match!");
System.out.println("Expected = " + secretState);
System.out.println("Got = " + value);
System.out.println();
}
// Trade the Request Token and Verfier for the Access Token
System.out.println("Trading the Request Token for an Access Token...");
final OAuth2AccessToken accessToken = service.getAccessToken(code);
System.out.println("Got the Access Token!");
System.out.println("(The raw response looks like this: " + accessToken.getRawResponse() + "')");
System.out.println();
但问题是我怎样才能获得授权码,谁能告诉我授权码是什么?
最佳答案
到目前为止,您已完成第 1 步:即创建授权 URL,告知服务器有关应用程序的信息(例如您的客户端 ID、重定向 URL 等详细信息)
在任何 OAuth 流程中,都涉及 3 方
- 用户
- 正在请求访问 token 的应用
- 服务提供商(本例中为 Github)
假设我是您管理的网站上的 GitHub 用户。您的网站想要访问我驻留在 GitHub 上的数据。如果没有访问 token
,您的网站无法直接从 GitHub 检索我的任何 protected 数据。
如何获取此访问 token ?
- 您的网站在 GitHub 上注册为客户端并获取
客户端 key
- 每当网站需要某个用户的
访问 token
时,它首先会通过向 GitHub 发送标识参数,通过authorization-url
来标识自己。 对于您的情况,您需要将该authorization-url
粘贴到浏览器中才能继续。在生产中,您的网站应将用户重定向到authorization-url
。 - 然后,Github 会验证网站的身份详细信息,如果一切正常,它会询问用户(在本例中为我)是否愿意向您的网站授予对我 protected 数据的访问权限。
- 如果我选择"is",GitHub 将调用您在
redirect_url
参数中指定的 URL 以及request-token
(又名authorization-code
) - 您的网站将读取此
请求 token
,使服务器调用 GitHub 并将其与访问 token
交换 - 一旦网站获得我的
访问 token
,它就可以向 GitHub 请求我的 protected 数据。
关于java - 通过 scribe 库在 java 中使用 github 的授权代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49233440/