谁能帮我了解可汗学院 API 的 OAuth 流程。可通过此链接访问: https ://github.com/Khan/khan-api/wiki/Khan-Academy-API-Authentication
我正在使用抄写员。
这是工作代码:
OAuthService serv = new ServiceBuilder().provider(KhanApi.class)
.apiKey("").apiSecret("")
.build();
Token token = new Token("", "");
OAuthRequest req = new OAuthRequest(Verb.GET,
"http://www.khanacademy.org/api/auth/request_token");
serv.signRequest(token, req);
Response resp = req.send();
System.out.println(resp.getBody());
收到响应后,您需要将用户重定向到该页面。然后在成功登录后,浏览器将响应一个包含请求令牌的 url...
以下是我尝试过但效果不佳的两种不同的方法及其结果:
1)
OAuthService serv = new ServiceBuilder().provider(KhanApi.class)
.apiKey("***").apiSecret("***")
.build();
Token token = serv.getRequestToken();
结果是:
线程“主”org.scribe.exceptions.OAuthException 中的异常:响应正文不正确。无法从中提取令牌和秘密:
<!DOCTYPE html>
<html>
<head>
<title>Login to Khan Academy</title>
<style>
#login-page {
padding-top: 18px;
}
.providers {
height: 100px;
}
.providers .provider .img-container {
height: 80px;
}
.horizontal-separator .separator-text {
background-color: white;
margin-left: 185px;
}
.pw-login {
width: 415px;
height: auto;
text-align: right;
}
.pw-login img.tree {
float: left;
}
</style>
</head>
<body>
<article id="login-page">
<div id="login-inner-content">
<form method="POST"
class="auth-form"
id="login-form"
action="https://khan-academy.appspot.com/login/mobileoauth">
<h3>Login to Khan Academy</h3>
<input type="hidden" name="oauth_map_id" value="889298340">
<input type="hidden" name="view" value="normal">
<ul class="providers">
<li class="provider action-gradient" title="Google">
</div>
</article>
</body>
</html>'
at org.scribe.extractors.TokenExtractorImpl.extract(TokenExtractorImpl.java:41)
at org.scribe.extractors.TokenExtractorImpl.extract(TokenExtractorImpl.java:27)
at org.scribe.oauth.OAuth10aServiceImpl.getRequestToken(OAuth10aServiceImpl.java:52)
at com.saeid.scribe.oauth.Main.main(Main.java:117)
响应是一个损坏的 html 文件的字符串。(在没有显示图像的意义上损坏......)
2)我也试过:
OAuthRequest req = new OAuthRequest(Verb.GET,
"http://www.khanacademy.org/api/auth/request_token");
Map<String, String> parameters = generateParameters("GET",
"http://www.khanacademy.org/api/auth/request_token");
req.addQuerystringParameter("oauth_consumer_key", parameters.get("oauth_consumer_key"));
req.addQuerystringParameter("oauth_nonce", parameters.get("oauth_nonce"));
req.addQuerystringParameter("oauth_signature", parameters.get("oauth_signature"));
req.addQuerystringParameter("oauth_signature_method", parameters.get("oauth_signature_method"));
req.addQuerystringParameter("oauth_timestamp", parameters.get("oauth_timestamp"));
req.addQuerystringParameter("oauth_version", parameters.get("oauth_version"));
Response res = req.send();
结果是:
OAuth error. Invalid signature. Expected signature base string: GET&http%3A%2F%2Fwww.khanacademy.org%2Fapi%2Fauth%2Frequest_token&oauth_consumer_key%3D***********%26oauth_nonce%3D1341526030%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1341526030%26oauth_version%3D1.0
我使用相同的基本字符串来生成签名,并且我使用消费者密钥作为 HMAC-SHA1 方法的密钥。
以下是 KhanAPI 的样子: import org.scribe.builder.api.DefaultApi10a; 导入 org.scribe.model.Token;
公共类 KhanApi 扩展 DefaultApi10a{
@Override
public String getAccessTokenEndpoint() {
return "http://www.khanacademy.org/api/auth/access_token";
}
@Override
public String getAuthorizationUrl(Token arg0) {
return "";
}
@Override
public String getRequestTokenEndpoint() {
return "http://www.khanacademy.org/api/auth/request_token";
}
}
谁能帮我?谢谢,