0

Android アプリケーションの Twitter OAuth を理解しようとしています。私の問題は、Twitter OAuth ブラウザーが開いたときにAuthorize Appボタンをクリックしても、コールバック URL にリダイレクトされ、そこで一時停止してアプリに戻らず、その後onNewIntent/onResumeメソッドのいずれも呼び出さないことです (両方を使用しようとしました)これら 2 つのメソッドは呼び出されず、どちらも呼び出されていません)。私のアクティビティ コードとマニフェスト XML ファイルは以下のとおりです。助けやヒントを本当にありがとう。ありがとうございました。

public class TwitterLoginActivity extends SherlockPreferenceActivity {

private static final String TAG = "TwitterDemo";

private static String CONSUMER_KEY = ""; // filled with my consumer key
private static String CONSUMER_SECRET = ""; //filled with my consumer secret
private static final String CALLBACK_SCHEME = "http";
private String CALLBACK_URL= CALLBACK_SCHEME+"://mjelajahprd.appspot.com/twitter/index";

private OAuthSignpostClient oauthClient;
private OAuthConsumer mConsumer;
private OAuthProvider mProvider;
private Twitter twitter;
private SharedPreferences prefs;
private String authUrl;
private WebView view;
private Boolean status;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    try {
        mConsumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY,
                CONSUMER_SECRET);

        mProvider = new DefaultOAuthProvider(
                "http://api.twitter.com/oauth/request_token",
                "http://api.twitter.com/oauth/access_token",
                "http://api.twitter.com/oauth/authorize");

        prefs = PreferenceManager.getDefaultSharedPreferences(this);
        String token = prefs.getString("token", null);
        String tokenSecret = prefs.getString("tokenSecret", null);

        if (token != null && tokenSecret != null) {
            mConsumer.setTokenWithSecret(token, tokenSecret);
            oauthClient = new OAuthSignpostClient(CONSUMER_KEY,
                    CONSUMER_SECRET, token, tokenSecret);
        } else {
            Log.d(TAG, "onCreate. Not Authenticated Yet ");
            new OauthAuthorizedTask().execute();


        }

    } catch (Exception e) {
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
    }
}

private class OauthAuthorizedTask extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {
        //String authUrl;
        String message = null;
        Log.d(TAG, "OAuthAuthorizeTask mConsumer: " + mConsumer);
        Log.d(TAG, "OAuthAuthorizeTask mProvider: " + mProvider);
        try {
            authUrl = mProvider.retrieveRequestToken(mConsumer,
                    CALLBACK_URL);
            Intent oauthIntent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse(authUrl));
            oauthIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            oauthIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            oauthIntent.addFlags(Intent.FLAG_FROM_BACKGROUND);
            startActivity(oauthIntent);


        } catch (OAuthMessageSignerException e) {
            message = "OAuthMessageSignerException";
            e.printStackTrace();
        } catch (OAuthNotAuthorizedException e) {
            message = "OAuthNotAuthorizedException";
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            message = "OAuthExpectationFailedException";
            e.printStackTrace();
        } catch (OAuthCommunicationException e) {
            message = "OAuthCommunicationException";
            e.printStackTrace();
        }
        return message;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if (result != null) {
            Toast.makeText(TwitterLoginActivity.this, result,
                    Toast.LENGTH_LONG).show();
        }
    }
}


@Override
public void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    Log.d(TAG, "intent: " + intent);

    // Check if this is a callback from OAuth
    Uri uri = intent.getData();
    if (uri != null && uri.getScheme().equals(CALLBACK_SCHEME)) {
        Log.d(TAG, "callback: " + uri.getPath());

        String verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
        Log.d(TAG, "verifier: " + verifier);
        Log.d(TAG, " xxxxxxxxxxx mConsumer access token: " + mConsumer.getToken());
        Log.d(TAG, " xxxxxxxxxxxx mConsumer access token secret: " + mConsumer.getTokenSecret());
        Log.d(TAG, " xxxxxxxxxxxxx OAuth.OAUTH_TOKEN: " + OAuth.OAUTH_TOKEN);
        Log.d(TAG, " xxxxxxxxxxxxx OAuth.OAUTH_TOKEN_SECRET: " + OAuth.OAUTH_TOKEN_SECRET);

        new RetrieveAccessTokenTask().execute(verifier);
    }
}


class RetrieveAccessTokenTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        String message = null;
        String oauthVerifier = params[0];
        try {
            // Get the token
            Log.d(TAG, " RetrieveAccessTokenTask mConsumer: " + mConsumer);
            Log.d(TAG, " RetrieveAccessTokenTask mProvider: " + mProvider);
            Log.d(TAG, " RetrieveAccessTokenTask verifier: " + oauthVerifier);
            mProvider.retrieveAccessToken(mConsumer, oauthVerifier);
            String token = mConsumer.getToken();
            String tokenSecret = mConsumer.getTokenSecret();
            mConsumer.setTokenWithSecret(token, tokenSecret);

            Log.d(TAG, String.format(
                    "verifier: %s, token: %s, tokenSecret: %s", oauthVerifier,
                    token, tokenSecret));

            // Store token in prefs
            prefs.edit().putString("token", token)
                    .putString("tokenSecret", tokenSecret).commit();

            // Make a Twitter object
            oauthClient = new OAuthSignpostClient(CONSUMER_KEY,
                    CONSUMER_SECRET, token, tokenSecret);
            twitter = new Twitter(null, oauthClient);

            Intent mainIntent = new Intent(TwitterLoginActivity.this, MainActivity.class);
            startActivity(mainIntent);

            Log.d(TAG, "token: " + token);
        } catch (OAuthMessageSignerException e) {
            message = "OAuthMessageSignerException";
            e.printStackTrace();
        } catch (OAuthNotAuthorizedException e) {
            message = "OAuthNotAuthorizedException";
            e.printStackTrace();
        } catch (OAuthExpectationFailedException e) {
            message = "OAuthExpectationFailedException";
            e.printStackTrace();
        } catch (OAuthCommunicationException e) {
            message = "OAuthCommunicationException";
            e.printStackTrace();
        }
        return message;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if (result != null) {
            Toast.makeText(TwitterLoginActivity.this, result,
                    Toast.LENGTH_LONG).show();
        }
    }
}

}

Manifest.xml (注: uses-permission android:name="android.permission.INTERNET" は既に設定されています)

    <activity
        android:name=".TwitterLoginActivity"
        android:label="@string/twitter"
        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:host="mjelajah-prd.appspot.com/twitter/index"
                android:scheme="http" >
            </data>
        </intent-filter>
    </activity>
4

1 に答える 1

2

で試してくださいandroid:launchMode="singleInstance"

private final String CALLBACKURL = "sosInternational:///Test";

<intent-filter>  
    <action android:name="android.intent.action.VIEW"></action>  
    <category android:name="android.intent.category.DEFAULT"></category>  
    <category android:name="android.intent.category.BROWSABLE"></category>  
    <data android:scheme="sosInternational" android:host="TwitterLoginActivity"></data>  
</intent-filter>

詳細については、このリンクを参照してください。

http://www.android10.org/index.php/articleslibraries/291-twitter-integration-in-your-android-application

Android の Twitter でのコールバックの問題

于 2012-10-05T06:20:11.553 に答える