无法在Tumblr上获取请求令牌 - 301错误

时间:2016-10-20 07:36:44

标签: android oauth tumblr

我试图在Android上使用Tumblr API 2.0(OAuth 1.0a)登录Tumblr。当我发送一个getRequestToken请求时,我得到了一个" 301永久移动"回应 - >崩溃。 OAuth1Service调试堆栈跟踪是:

10-20 10:20:34.455 2154-2387/com.package.android I/System.out: obtaining request token from http://www.tumblr.com/oauth/request_token
10-20 10:20:34.459 2154-2387/com.package.android I/System.out: setting oauth_callback to http://127.0.0.1
10-20 10:20:34.465 2154-2387/com.package.android I/System.out: generating signature...
10-20 10:20:34.466 2154-2387/com.package.android I/System.out: using base64 encoder: CommonsCodec
10-20 10:20:34.475 2154-2387/com.package.android I/System.out: base string is: POST&http%3A%2F%2Fwww.tumblr.com%2Foauth%2Frequest_token&oauth_callback%3Dhttp%253A%252F%252F127.0.0.1%26oauth_consumer_key%3DkizvGnMKiwPzfKNdLoGe4UXrO4EaCXa5UsEMOVvP6KkS2xZ8QQ%26oauth_nonce%3D1243319427%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1476948034%26oauth_version%3D1.0
10-20 10:20:34.475 2154-2387/com.package.android I/System.out: signature is: Jm8oRzCRHwuKiBg800CjW6tpioc=
10-20 10:20:34.476 2154-2387/com.package.android I/System.out: appended additional OAuth parameters: { oauth_signature_method -> HMAC-SHA1 , oauth_consumer_key -> kizvGnMKiwPzfKNdLoGe4UXrO4EaCXa5UsEMOVvP6KkS2xZ8QQ , oauth_version -> 1.0 , oauth_timestamp -> 1476948034 , oauth_nonce -> 1243319427 , oauth_callback -> http://127.0.0.1 , oauth_signature -> Jm8oRzCRHwuKiBg800CjW6tpioc= }
10-20 10:20:34.477 2154-2387/com.package.android I/System.out: using Http Header signature
10-20 10:20:34.484 2154-2387/com.package.android I/System.out: sending request...
10-20 10:20:34.488 2154-2387/com.package.android D/NetworkSecurityConfig: No Network Security Config specified, using platform default
10-20 10:20:34.868 2154-2387/com.package.android I/System.out: response status code: 301
10-20 10:20:34.868 2154-2387/com.package.android I/System.out: response body: <html>
10-20 10:20:34.868 2154-2387/com.package.android I/System.out: <head><title>301 Moved Permanently</title></head>
10-20 10:20:34.868 2154-2387/com.package.android I/System.out: <body bgcolor="white">
10-20 10:20:34.868 2154-2387/com.package.android I/System.out: <center><h1>301 Moved Permanently</h1></center>
10-20 10:20:34.868 2154-2387/com.package.android I/System.out: <hr><center>nginx</center>
10-20 10:20:34.868 2154-2387/com.package.android I/System.out: </body>
10-20 10:20:34.868 2154-2387/com.package.android I/System.out: </html>
10-20 10:20:34.870 2154-2387/com.package.android W/System.err: com.github.scribejava.core.exceptions.OAuthException: Response body is incorrect. Can't extract token and secret from this: '<html>
10-20 10:20:34.870 2154-2387/com.package.android W/System.err: <head><title>301 Moved Permanently</title></head>
10-20 10:20:34.870 2154-2387/com.package.android W/System.err: <body bgcolor="white">
10-20 10:20:34.870 2154-2387/com.package.android W/System.err: <center><h1>301 Moved Permanently</h1></center>
10-20 10:20:34.870 2154-2387/com.package.android W/System.err: <hr><center>nginx</center>
10-20 10:20:34.870 2154-2387/com.package.android W/System.err: </body>
10-20 10:20:34.870 2154-2387/com.package.android W/System.err: </html>

(消费者密钥和秘密是真实的,但来自虚拟账户)

奇怪的是,昨天一切都还可以,我正在成功收到请求令牌。可能是导致问题的原因是什么?

更新/解决 - 问题出现在ScribeJava TumblrApi模型中。默认情况下,它包含带有&#34; http&#34;的网址。前缀,但实际的Tumblr API需要&#34; https&#34;

1 个答案:

答案 0 :(得分:0)

如果其他人遇到问题...创建一个新的&#34;自定义api&#34;

public class TumblrHTTPSApi extends DefaultApi10a {
    private static final String AUTHORIZE_URL =   "https://www.tumblr.com/oauth/authorize?token=%s";

    protected TumblrHTTPSApi() {
    }

    private static class InstanceHolder {
        private static final TumblrHTTPSApi INSTANCE = new TumblrHTTPSApi();
    }

    public static TumblrHTTPSApi instance() {
        return InstanceHolder.INSTANCE;
    }

    @Override
    public String getAccessTokenEndpoint(){
        return "https://www.tumblr.com/oauth/access_token";
    }

    @Override
    public String getRequestTokenEndpoint() {
        return "https://www.tumblr.com/oauth/request_token";
    }

    @Override
    public String getAuthorizationUrl(OAuth1RequestToken requestToken) {
       return String.format(AUTHORIZE_URL, requestToken.getToken());
    }
}

这个&#34;新API&#34;然后可以像往常一样使用:

// create new oauth service
final OAuth10aService service = new ServiceBuilder()
    .apiKey(consumerKey)
    .apiSecret(consumerSecret)
    .callback(callbackUrl)
    .build(TumblrHTTPSApi.instance());