gpt4 book ai didi

java - Android Twitter4J 自动检索 OAuth 和 oauth_token_secret?

转载 作者:太空狗 更新时间:2023-10-29 16:07:33 26 4
gpt4 key购买 nike

我已经在我的 Android 应用程序中实现了 Twitter4J,它运行良好。但是在我使用 Twitter 登录页面登录后,它要求用户手动输入 PIN 码。

在 Twitter4J 类中,有一种方法可以检索和存储(到共享首选项中)OAuth 和 oauth_token_secret。

/**
* Retrieve the oauth_verifier, and store the oauth and oauth_token_secret
* for future API calls.
*/
@Override
protected Void doInBackground(Uri...params) {
final Uri uri = params[0];
final String oauth_verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);

try {
provider.retrieveAccessToken(consumer, oauth_verifier);

final Editor edit = prefs.edit();
edit.putString(OAuth.OAUTH_TOKEN, consumer.getToken());
edit.putString(OAuth.OAUTH_TOKEN_SECRET, consumer.getTokenSecret());
edit.commit();

String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");

consumer.setTokenWithSecret(token, secret);
context.startActivity(new Intent(context,MainAct.class));

executeAfterAccessTokenRetrieval();

Log.i(TAG, "OAuth - Access Token Retrieved");

} catch (Exception e) {
Log.e(TAG, "OAuth - Access Token Retrieval Error", e);
}

return null;
}

list - PrepareRequestTokenActivity

<activity android:name=".PrepareRequestTokenActivity" android:launchMode="singleTask">>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="x-oauthflow-twitter" android:host="callback" />
</intent-filter>
</activity>

我不明白为什么它不检索 OAUTH_TOKENOAUTH_SECRET。如何在不输入 PIN 码的情况下进行授权?我做错了什么吗?

请帮帮我。

谢谢

最佳答案

嘿,这段代码可能会有帮助,

AccessToken accessToken = getAccessToken();

Configuration conf = new ConfigurationBuilder()
.setOAuthConsumerKey(TwitterConstants.CONSUMER_KEY)
.setOAuthConsumerSecret(TwitterConstants.CONSUMER_SECRET)
.setOAuthAccessToken(accessToken.getToken())
.setOAuthAccessTokenSecret(accessToken.getTokenSecret())
.build();

OAuthAuthorization auth = new OAuthAuthorization(conf,
conf.getOAuthConsumerKey(), conf.getOAuthConsumerSecret(),
new AccessToken(conf.getOAuthAccessToken(),
conf.getOAuthAccessTokenSecret()));

并获取 AccessToken,

public AccessToken getAccessToken() {
String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
String tokenSecret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");

if (token != null && tokenSecret != null)
return new AccessToken(token, tokenSecret);
else
return null;
}

关于java - Android Twitter4J 自动检索 OAuth 和 oauth_token_secret?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10618330/

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