I use signpost-oauth do oauth in android, and after login on the web page, return to the Activity, the provider is null sometimes.
public class LoginActivity extends Activity implements View.OnClickListener {
private static OAuthConsumer consumer;
private static OAuthProvider provider;
private static final Uri CALLBACK= Uri.parse("xxx://callback?");
...
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
final Uri uri = intent.getData();
if (uri != null && uri.toString().startsWith(CALLBACK)) {
final String pin = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
try {
//provider.setOAuth10a(true);
provider.retrieveAccessToken(consumer, pin);
} catch (final Exception e) {
}
}
}
...
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.oAuthLoginBtn:
new Thread(new Runnable() {
public void run() {
LoginActivity.consumer = new CommonsHttpOAuthConsumer(
apiKey, appSecret);
LoginActivity.provider = new CommonsHttpOAuthProvider(
oauthUrl + Data.REQUEST_TOKEN_URL, oauthUrl
+ Data.ACCESS_TOKEN_URL, oauthUrl
+ Data.AUTHORIZE_URL);
LoginActivity.provider.setOAuth10a(true);
try {
final String authUrl = LoginActivity.provider
.retrieveRequestToken(LoginActivity.consumer,
CALLBACK);
final Uri uri = Uri.parse(authUrl);
final Intent it = new Intent(Intent.ACTION_VIEW, uri);
LoginActivity.this.startActivity(it);
} catch (final Exception e) {
}
}
}).start();
break;
}
}
and the manifest.xml
<activity android:name=".ui.LoginActivity" android:label="@string/activity_login"
android:launchMode="singleInstance" android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:na开发者_运维问答me="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="xxx" />
</intent-filter>
</activity>
I don't know why the provider is null sometimes.
I think you are not receiving the correct oauth_token and oauth_verifier. define a host
<data android:scheme="xxx" android:host="twitt" />
the you will receive the correct values.
xxx://twitt?oauth_token=it23W5yOy13x6s1ox6kpaOow2aM70OKTW3WTy4h8Q4&oauth_verifier=gRuGGE4osgjTwltooi9MG4u7a4EbxAsIxvVjZqd9dA
Maybe <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
and/or <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
is missing?
精彩评论