gpt4 book ai didi

android - 使用 SignPost 在 Android 中登录 Tumblr

转载 作者:行者123 更新时间:2023-11-29 21:11:05 26 4
gpt4 key购买 nike

我正在尝试按照以下示例在我的 android 应用程序中实现 tumblr 登录:https://github.com/jansanz/TumblrOAuthDemo .

我可以获得请求 token 。但是当我试图检索 AccessToken 时,它给了我一个异常。这是代码:

public class TumblrDemoActivity extends Activity {

private static final String TAG = "TumblrDemo";

private static final String PREFS = "prefs_tumblr";
private static final String PREF_TOKEN = "pref_token_tumblr";
private static final String PREF_TOKEN_SECRET = "pref_token_secret_tumblr";

private static final String REQUEST_TOKEN_URL = "http://www.tumblr.com/oauth/request_token";
private static final String ACCESS_TOKEN_URL = "http://www.tumblr.com/oauth/access_token";
private static final String AUTH_URL = "http://www.tumblr.com/oauth/authorize";

// Taken from Tumblr app registration
private static final String CONSUMER_KEY = "consumer key from tumblr app";
private static final String CONSUMER_SECRET = "consumer secret from tumblr app";

private static final String CALLBACK_URL = "tumblrdemo://www.android-ios-tutorials.com";//"tumblrdemo://tumblrdemo.com/ok";

CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
CommonsHttpOAuthProvider provider = new CommonsHttpOAuthProvider(
REQUEST_TOKEN_URL,
ACCESS_TOKEN_URL,
AUTH_URL);

SharedPreferences prefs;

String token;
String tokenSecret;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
prefs = getSharedPreferences(PREFS, Context.MODE_PRIVATE);
consumer.setMessageSigner(new HmacSha1MessageSigner());
// It uses this signature by default
// consumer.setMessageSigner(new HmacSha1MessageSigner());

// To get the oauth token after the user has granted permissions
Uri uri = this.getIntent().getData();
if (uri != null) {

Log.d(TAG, "accessToken : "+consumer.getToken());
Log.d(TAG, "accessTokenSecret : "+consumer.getTokenSecret());

//retrieve token and tokenSecret saved in retrieveRequestToken() call
this.token = prefs.getString(PREF_TOKEN, null);
this.tokenSecret = prefs.getString(PREF_TOKEN_SECRET, null);
Log.d(TAG, "this.token : "+token);
Log.d(TAG, "this.tokenSecret : "+tokenSecret);

consumer.setTokenWithSecret(token, tokenSecret);

Log.d(TAG, "consumer.token : "+consumer.getToken());
Log.d(TAG, "consumer.tokenSecret : "+consumer.getTokenSecret());

String token = uri.getQueryParameter("oauth_token");
String verifier = uri.getQueryParameter("oauth_verifier");

Log.v(TAG, "Token:" +token);
Log.v(TAG, "Verifier:" +verifier);

try {
provider.retrieveAccessToken(consumer, verifier);
Log.d(TAG, "accessToken retrieveAccessToken : "+consumer.getToken());
Log.d(TAG, "accessTokenSecret retrieveAccessToken : "+consumer.getTokenSecret());

} catch (OAuthMessageSignerException e) {
e.printStackTrace();
} catch (OAuthNotAuthorizedException e) {
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
e.printStackTrace();
} catch (OAuthCommunicationException e) {
e.printStackTrace();
}
} else {
String authUrl;
try {
authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL);
Log.v(TAG, "Auth url:" + authUrl);
Log.d(TAG, "accessToken : "+consumer.getToken());
Log.d(TAG, "accessTokenSecret : "+consumer.getTokenSecret());

//save tokens in preferences
SharedPreferences.Editor editor = prefs.edit();
editor.putString(PREF_TOKEN, consumer.getToken());
editor.putString(PREF_TOKEN_SECRET, consumer.getTokenSecret());
editor.commit();

startActivity(new Intent("android.intent.action.VIEW", Uri.parse(authUrl)));

} catch (OAuthMessageSignerException e) {
e.printStackTrace();
} catch (OAuthNotAuthorizedException e) {
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
e.printStackTrace();
} catch (OAuthCommunicationException e) {
e.printStackTrace();
}
}
}

@Override
protected void onResume() {
super.onResume();
Log.v(TAG, "onResume");
}

第一次调用 provider.retrieveRequestToken() 就可以了。我可以检索 token 和 tokenSecret。但是当我尝试通过调用 provider.retrieveAccessToken(consumer,oauth_verifier); 获取 accessToken 时,我得到了一个 OAuthCommunicationExceptionOAuthCommunicationException 的堆栈跟踪是:

    04-02 11:34:14.735: W/System.err(19535): oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: Service provider responded in error: 400 (Bad Request)
04-02 11:34:14.735: W/System.err(19535): at oauth.signpost.AbstractOAuthProvider.retrieveToken(AbstractOAuthProvider.java:218)
04-02 11:34:14.735: W/System.err(19535): at oauth.signpost.AbstractOAuthProvider.retrieveAccessToken(AbstractOAuthProvider.java:108)
04-02 11:34:14.735: W/System.err(19535): at org.jfsd.tumblrdemo.TumblrDemoActivity.onCreate(TumblrDemoActivity.java:87)
04-02 11:34:14.735: W/System.err(19535): at android.app.Activity.performCreate(Activity.java:5066)
04-02 11:34:14.745: W/System.err(19535): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102)
04-02 11:34:14.745: W/System.err(19535): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
04-02 11:34:14.745: W/System.err(19535): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
04-02 11:34:14.745: W/System.err(19535): at android.app.ActivityThread.access$600(ActivityThread.java:151)
04-02 11:34:14.745: W/System.err(19535): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1330)
04-02 11:34:14.745: W/System.err(19535): at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 11:34:14.755: W/System.err(19535): at android.os.Looper.loop(Looper.java:155)
04-02 11:34:14.755: W/System.err(19535): at android.app.ActivityThread.main(ActivityThread.java:5536)
04-02 11:34:14.755: W/System.err(19535): at java.lang.reflect.Method.invokeNative(Native Method)
04-02 11:34:14.755: W/System.err(19535): at java.lang.reflect.Method.invoke(Method.java:511)
04-02 11:34:14.755: W/System.err(19535): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
04-02 11:34:14.755: W/System.err(19535): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
04-02 11:34:14.755: W/System.err(19535): at dalvik.system.NativeStart.main(Native Method)
04-02 11:34:14.765: W/System.err(19535): Caused by: oauth.signpost.exception.OAuthCommunicationException: Service provider responded in error: 400 (Bad Request)
04-02 11:34:14.765: W/System.err(19535): at oauth.signpost.AbstractOAuthProvider.handleUnexpectedResponse(AbstractOAuthProvider.java:245)
04-02 11:34:14.765: W/System.err(19535): at oauth.signpost.AbstractOAuthProvider.retrieveToken(AbstractOAuthProvider.java:193)
04-02 11:34:14.765: W/System.err(19535): ... 16 more

感谢任何帮助。

最佳答案

我使用 oauth sign post 和 jumblr 创建了一个用于 Tumblr 登录的小型库。你可以在这里找到它:

Tumblr Login API Using oauth sign post

我希望这个库对遇到同样问题的其他人有所帮助。

关于android - 使用 SignPost 在 Android 中登录 Tumblr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22810226/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com