HTTP / 1.1 401 Несанкционированная ошибка при вызове HttpGet с клиентом OAuthConsumer - PullRequest
0 голосов
/ 12 ноября 2018

Я получаю 401 несанкционированную ошибку при использовании вызова Http GET с клиентом OAuthConsumer.

У меня есть все необходимые параметры для авторизации Oauth 1.0.

Consumer Key = "XXX"
Consumer Secret = "YYY"
oauth_signature_method = "HMAC-SHA1"
oauth_timestamp = "calculated timestamp"
oauth_nonce = "calculated nonce"
oauth_version = 1.0
oauth_signature = "calculated signature"

Тот же код Ouath 1.0в Python работает хорошо для меня, просто используя потребительский ключ, секретный ключ и signature_type = 'auth_header' и вызывая request.get ().Вот мой код на Python ниже:

import requests
from requests_oauthlib import OAuth1

url = "XXX"
header_auth = OAuth1('consumer_key','consumer_secret', signature_type='auth_header')
response = requests.get(url, auth=header_auth)
print(response.status_code)
print(response.content)

Вот мой фрагмент кода на Java.Не могли бы вы подсказать, где я ошибаюсь?

открытый класс OauthConsumerClient {

@SuppressWarnings("deprecation")
public static void main(String[] args) {

        String url = "XXX";

        DefaultHttpClient httpClient = new DefaultHttpClient();
        httpClient.getParams().setParameter("http.protocol.content-charset", "UTF-8");      
        HttpRequestBase httpRequest = null;
        URI uri = null;
        HttpResponse httpResponse = null;
        OAuthConsumer oAuthConsumer = new CommonsHttpOAuthConsumer("consumer_key", "consumer_secret");
        oAuthConsumer.setSigningStrategy(new AuthorizationHeaderSigningStrategy());

        try {
            uri = new URI(url);
            httpRequest = new HttpGet(uri);
            httpRequest.setHeader("Content-Type", "application/json");
            oAuthConsumer.sign(httpRequest);
            HttpHost target = new HttpHost(uri.getHost(), -1, uri.getScheme());
            httpResponse = httpClient.execute(target, httpRequest);
            System.out.println("Connection status : " + httpResponse.getStatusLine());
            System.out.println("Connection status code : " + httpResponse.getStatusLine().getStatusCode());

        } catch (Exception e) {
            System.out.println("Exception occured");
        }
        InputStream inputStraem = httpResponse.getEntity().getContent();
        StringWriter writer = new StringWriter();
        IOUtils.copy(inputStraem, writer, "UTF-8");
        String output = writer.toString();
        System.out.println("Connection response : " + output);
}

}

Output 
-------------------------------------------------------------------------

16:13:43.064 [main] DEBUG o.a.h.impl.client.DefaultHttpClient - Authentication required
16:13:43.064 [main] DEBUG o.a.h.impl.client.DefaultHttpClient - "URL" requested authentication
16:13:43.064 [main] DEBUG o.a.h.i.c.TargetAuthenticationStrategy - Authentication schemes in the order of preference: [Negotiate, Kerberos, NTLM, Digest, Basic]
16:13:43.064 [main] DEBUG o.a.h.i.c.TargetAuthenticationStrategy - Challenge for Negotiate authentication scheme not available
16:13:43.064 [main] DEBUG o.a.h.i.c.TargetAuthenticationStrategy - Challenge for Kerberos authentication scheme not available
16:13:43.064 [main] DEBUG o.a.h.i.c.TargetAuthenticationStrategy - Challenge for NTLM authentication scheme not available
16:13:43.064 [main] DEBUG o.a.h.i.c.TargetAuthenticationStrategy - Challenge for Digest authentication scheme not available
16:13:43.064 [main] DEBUG o.a.h.i.c.TargetAuthenticationStrategy - Challenge for Basic authentication scheme not available
Connection status : HTTP/1.1 401 Unauthorized
Connection status code : 401
16:13:43.076 [main] DEBUG org.apache.http.wire -  << "  ["The request must be signed"]"
16:13:43.076 [main] DEBUG o.a.h.i.c.BasicClientConnectionManager - Releasing connection org.apache.http.impl.conn.ManagedClientConnectionImpl@78691363
16:13:43.076 [main] DEBUG o.a.h.i.c.BasicClientConnectionManager - Connection can be kept alive indefinitely
Connection response :   ["The request must be signed"]

ПРИМЕЧАНИЕ. Когда я нажимаю на тот же URL в почтальоне сТип авторизации Oauth 1.0.Я получаю код ответа 200 ОК с телом.

Дайте мне знать, если вам нужна дополнительная информация.Большое спасибо за вашу помощь !!!

...