如何将经过身份验证的请求发送到Discogs Api

时间:2016-01-25 11:57:24

标签: java authentication oauth discogs-api

我跟随OAuth 1.0 Protocol,因此我可以在Auth flow的第5步中向/ oauth / identity端点发送经过身份验证的请求。

在文档中,提出请求涉及包括几个参数,如您所见[{3}}。

我的要求是:

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来自Auth Flow指南的第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);
        }
    });

}

但我得到的是:{" message":"您必须通过身份验证才能访问此资源。"}

1 个答案:

答案 0 :(得分:0)

使用OAuth库而不是手动生成这些请求可能会让您头疼。更多信息请参阅http://tools.ietf.org/html/rfc5849#page-14

相关问题