【发布时间】:2016-05-01 17:29:31
【问题描述】:
我正在关注OAuth 1.0 Protocol,因此我可以像Auth flow 的第5 步那样向/oauth/identity 端点发送经过身份验证的请求。
从文档中,提出请求涉及包括几个参数,如您所见 here。
我的请求是什么样的:
http://api.discogs.com/oauth/identity?
oauth_consumer_key=CONSUMER_KEY&
oauth_nonce=1453720099377&
oauth_signature=CONSUMER_SECRET&ACCESS_TOKEN_SECRET&
oauth_signature_method=PLAINTEXT&
oauth_timestamp=1453720099377&
oauth_token=ACCESS_TOKEN
其中 ACCESS_TOKEN 和 ACCESS_TOKEN_SECRET 派生自身份验证流程指南的第 4 步,并且 我的 Discogs 开发者设置中的 CONSUMER_SECRET 和 CONSUMER_KEY。
我的代码在 Java 中的样子,因为这是针对 android Discogs 客户端的:
public void getIdentity() {
httpClient.addHeader("Content-Type", "application/x-www-form-urlencoded");
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("OAuth oauth_consumer_key=\"").append(Constants.CONSUMER_KEY).append("\",");
stringBuilder.append("oauth_nonce=\"").append(String.valueOf(System.currentTimeMillis())).append("\",");
stringBuilder.append("oauth_signature=\"").append(Constants.CONSUMER_SECRET).append(authAccessSecretToken).append("\",");
stringBuilder.append("oauth_signature_method=\"PLAINTEXT\",");
stringBuilder.append("oauth_timestamp=\"").append(String.valueOf(System.currentTimeMillis())).append("\",");
stringBuilder.append("oauth_token=\"").append(authAccessToken).append("\"");
httpClient.addHeader("Authorization", stringBuilder.toString());
httpClient.addHeader("User-Agent", "Discs/1.0 +https://jb.com");
httpClient.get(identityURL, null, new TextHttpResponseHandler() {
@Override
public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
Log.d(TAG, "onFailure IDENTITY " + i + " Throwable = " + throwable);
Log.d(TAG, "onFailure IDENTITY " + s);
}
@Override
public void onSuccess(int i, Header[] headers, String s) {
Log.i(TAG, "onSuccess IDENTITY -- String= " + s);
}
});
}
但我得到的是:{“消息”:“您必须进行身份验证才能访问此资源。”}
【问题讨论】:
标签: java authentication oauth discogs-api