Scribe Tumblr响应正文不正确。无法从中提取令牌和秘密:'oauth_verifier缺失或无效'。

时间:2014-01-23 20:48:15

标签: tumblr scribe

我在使用Scribe验证Tumblr时遇到问题。我跟着我在SO上找到的一个例子,但我一直得到“org.scribe.exceptions.OAuthException:响应体不正确。无法从中提取令牌和秘密:'缺少或无效的oauth_verifier。'”

有什么想法吗?帮助将非常感谢!感谢。

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;
import org.scribe.builder.ServiceBuilder;
import org.scribe.builder.api.TumblrApi;
import org.scribe.model.*;
import org.scribe.oauth.OAuthService;

public class MyActivity extends Activity {
    private static OAuthService service;
    private Token requestToken;
    private Verifier verifier;
    private static final String PROTECTED_RESOURCE_URL = "http://api.tumblr.com/v2/user/info";
    private static Boolean authInProgress = false;
    public WebView TumblrWebView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        setContentView(R.layout.main);

        if ( ! authInProgress ) {
            authInProgress = true;
            service = new ServiceBuilder()
                    .provider(TumblrApi.class)
                    .apiKey("xxx")
                    .apiSecret("yyy")
                    .callback("thisapp://tumblr")
                    .build();

            // Obtain the Request Token
            Log.d("Tumblr", "Fetching the Request Token...");
            requestToken = service.getRequestToken();
            Log.d("Tumblr", "Got the Request Token!");

            Log.d("Tumblr", "Now go and authorize Scribe here:");
            Log.d("Tumblr", ""+service.getAuthorizationUrl(requestToken));

            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse( service.getAuthorizationUrl(requestToken) ));
            startActivity(browserIntent);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (this.getIntent()!=null && this.getIntent().getData()!=null){
            Uri uri = this.getIntent().getData();
            if (uri != null && uri.toString().startsWith("thisapp://tumblr")) {
                verifier = new Verifier ( uri.getQueryParameter("oauth_verifier") );
                // Trade the Request Token and Verfier for the Access Token
                requestToken = service.getRequestToken();
                Log.d("Tumblr", "Trading the Request Token for an Access Token...");
                Log.d("Tumblr", " ---> Request Token: " + requestToken.getToken());
                Log.d("Tumblr", " ---> Request Token Secret: " + requestToken.getSecret());
                Log.d("Tumblr", " ---> Verifier: " + verifier.getValue());
                Token accessToken = service.getAccessToken(requestToken, verifier);
                Log.d("Tumblr", "Got the Access Token!");
                Log.d("Tumblr", "(if you're curious it looks like this: " + accessToken + " )");

                // Now let's go and ask for a protected resource!
                Log.d("Tumblr", "Now we're going to access a protected resource...");
                OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
                service.signRequest(accessToken, request);
                Response response = request.send();
                Log.d("Tumblr", "Got it! Lets see what we found...");
                Log.d("Tumblr", ""+response.getBody());

                Log.d("Tumblr", "Thats it man! Go and build something awesome with Scribe! :)");
            }
        }

    }
}

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。这是因为不同的请求令牌造成的。

Tumblr的OAuth工作方式:

  • 您的申请要求requestToken并生成AuthorizationUrl
  • 将用户重定向到tumblrs页面进行身份验证。
  • 用户授予所有权限后,tumblr会使用oauth_verifier代码重定向回回调网址。
  • 现在,您尝试根据accessTokenoauth_verifier生成requestToken
  • 现在是扭曲,您需要确保requestToken与获取网址时使用的相同,否则会抛出错误,因为无法提取令牌。