gpt4 book ai didi

android - 从 Android 的 Twitter OAuth 中提取 URI

转载 作者:行者123 更新时间:2023-11-30 04:13:33 25 4
gpt4 key购买 nike

我有以下功能来管理登录:

private static final String CALLBACK_URL = "myapp://twitter";

private void loginViaOAuth() {
try {
consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
provider = new CommonsHttpOAuthProvider(REQUEST_URL, ACCESS_TOKEN_URL, AUTH_URL);
provider.setOAuth10a(true);
String authURL = provider.retrieveRequestToken(consumer, CALLBACK_URL);
this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authURL)));
} catch (Exception e) {
e.printStackTrace();
}
}

和 Manifest.xml

<activity android:name=".Twitterconnect"
android:launchMode="singleTask"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name" >
<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="myapp" android:host="twitter" />
</intent-filter>
</activity>

我得到的回应是:

05-03 11:24:03.968: INFO/ActivityManager(59): Starting activity: Intent { 
act=android.intent.action.VIEW
cat=[android.intent.category.BROWSABLE]
dat=myapp://twitter?oauth_token=tJpJHOOwoTGMwdvHyYbfX2tyHKOp0Y2kdRRZf3sM&oauth_verifier=xc49oM8eVVmK46ZSLz2RMT2uqXn3SxrMxf5ZAMXaD2Y
cmp=my.app/.Twitterconnect

这是我的 OnResume 函数:

@Override
protected void onResume() {
super.onResume();
Uri uri = this.getIntent().getData();
if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
Log.d("verifier:", verifier);
try {
provider.setOAuth10a(true);
provider.retrieveAccessToken(consumer, verifier);
ACCESS_KEY = consumer.getToken();
ACCESS_SECRET = consumer.getTokenSecret();
AccessToken a = new AccessToken(ACCESS_KEY, ACCESS_SECRET);
// initialize Twitter4J
twitter = new Twitter();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
twitter.setOAuthAccessToken(a);
Log.e("TWITTER", "step1");
twitter.updateStatus("Just shared the ".concat(tweet).concat(" on Twitter!"));
Log.e("TWITTER", ""+buttonTwitterShare.getVisibility());
buttonTwitterShare.setVisibility(View.GONE);
Log.e("TWITTER", "step2");
Log.e("TWITTER", ""+buttonTwitterShare.getVisibility());
Toast.makeText(getBaseContext(), "Post successfully tweeted!", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
}

我的问题是,为什么无论我使用通过 Uri uri = this.getIntent().getData(); 获取的 URI 登录什么配置文件,它始终为 NULL?如何从响应中提取 oauth_tokenoauth_verifier:dat=myapp://twitter?oauth_token=tJpJHOOwoTGMwdvHyYbfX2tyHKOp0Y2kdRRZf3sM&oauth_verifier=xc49oM8eVVmK46ZSLz2RMT2uqXn3 SxrMxf5ZAMXaD2Y ?

最佳答案

这可能对你有用

    Uri uri = this.getIntent().getData();
if (uri == null) {
return null;
}
String token = uri.getQueryParameter("oauth_token");
String verifier = uri.getQueryParameter("oauth_verifier");

连这两行

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

正如您已经在获取 uri 应该做的那样。

关于android - 从 Android 的 Twitter OAuth 中提取 URI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10428472/

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