【发布时间】:2011-05-09 23:02:05
【问题描述】:
我有很多关于 O_Auth 的示例,但没有一个工作正常,我想唯一的问题是每个示例中的回调 URL,我收到 401 错误,例如使用者密钥或签名不匹配。我已经在 401 上看到了很多例子,唯一对我有利的是我的回调 url。
android app cannot connect to twitter。 Getting 401 when requesting access token with signpost within android with-signpost-within-android Android Dev - Callback URL not working... (0_o)
我已经看过所有这些示例以及更多示例。
但我仍然没有明白我的意思,如果在申请表时我在回调 url 使用http://www.meomyo.com,那么我应该在我的编码中使用它 像 android:scheme="?"机器人:主机=“?” 目前我正在使用其他示例回调
//private static final Uri CALLBACK_URI = Uri.parse("bloa-app://twitt");
private static final Uri CALLBACK_URI = Uri.parse("twitterapp://connect");
我有消费者密钥和秘密密钥,但如果是回调 url,我会卡在它上面。 如果有人想要,我可以提供我的消费者以及密钥。
public class OAuth extends Activity {
private static final String APP = "OAUTH";
private Twitter twitter;
private OAuthProvider provider;
private CommonsHttpOAuthConsumer consumer;
private String CONSUMER_KEY = "Xh3o8Gh1JQnklkUnTvvA";
private String CONSUMER_SECRET = "SCLy6yoUSth53goAsYYkoqR4ZuBoaInyJXsm5PQR11I";
private String CALLBACK_URL = "merabharatmahan://piyush";
private TextView tweetTextView;
private Button buttonLogin;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tweetTextView = (TextView)findViewById(R.id.tweet);
buttonLogin = (Button)findViewById(R.id.ButtonLogin);
buttonLogin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
askOAuth();
}
});
}
/**
* Open the browser and asks the user to authorize the app.
* Afterwards, we redirect the user back here!
*/
private void askOAuth() {
try {
consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
provider = new DefaultOAuthProvider("http://twitter.com/oauth/request_token",
"http://twitter.com/oauth/access_token",
"http://twitter.com/oauth/authorize");
String authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL);
Toast.makeText(this, "Please authorize this app!", Toast.LENGTH_LONG).show();
this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
} catch (Exception e) {
Log.e(APP, e.getMessage());
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
/**
* As soon as the user successfully authorized the app, we are notified
* here. Now we need to get the verifier from the callback URL, retrieve
* token and token_secret and feed them to twitter4j (as well as
* consumer key and secret).
*/
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Uri uri = intent.getData();
if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
try {
// this will populate token and token_secret in consumer
provider.retrieveAccessToken(consumer, verifier);
// TODO: you might want to store token and token_secret in you app settings!!!!!!!!
AccessToken a = new AccessToken(consumer.getToken(), consumer.getTokenSecret());
// initialize Twitter4J
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
twitter.setOAuthAccessToken(a);
// create a tweet
Date d = new Date(System.currentTimeMillis());
String tweet = "#OAuth working! " + d.toLocaleString();
// send the tweet
twitter.updateStatus(tweet);
// feedback for the user...
tweetTextView.setText(tweet);
Toast.makeText(this, tweet, Toast.LENGTH_LONG).show();
buttonLogin.setVisibility(Button.GONE);
} catch (Exception e) {
Log.e(APP, e.getMessage());
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
}
清单代码是
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".OAuth"
android:label="@string/app_name"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<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="merabharatmahan" android:host="piyush" />
</intent-filter>
</activity>
</application
现在我与服务提供商的通信失败:收到的身份验证质询为空。这是我放在这里的最简单的例子。
【问题讨论】:
-
按照上面的清单,首先改变 android:launchMode="singleTask", android:scheme 和 host 在我看来没问题。
-
让我们知道您在 android 中使用的是哪个身份验证 jar?我正在使用 twitter4j、signpost-core 和 signpost-commonshttp jar 来实现 oAuth。
-
try { consumer.setTokenWithSecret(tempToken, tempSecretKey); provider.retrieveAccessToken(consumer, verifier); HttpParameters params = provider.getResponseParameters(); String userName = params.getFirst("screen_name"); String returnToken = consumer.getToken(); String returnSecret = consumer.getTokenSecret();
标签: android