tags:

views:

37

answers:

1

Hi guys,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:name="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.

A: 

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
Jorgesys
kmlzkma
hi I have moved this line Provider.setOAuth10a(true); and define CALLBACK as a static variable!, and i have a question why don't you use onResume() instead of onNewIntent()?
Jorgesys
hi,i found it is still null sometimes.I used launchMode="singleInstance" in manifest.xml, so have to use onNewIntent() to receive that.
kmlzkma