简体   繁体   中英

Twitter4j OAuth callback succeeds, but test tweet fails

Having a rough time with OAuth in Twitter4j. Before this, I had the problem detailed in #3255153 and a 401 error, but finally fixed those, and ran into a harder to solve problem.

The Twitter application authorization page launches in a browser, I log in, and approve the application for my account. It then redirects back to the application, and nothing happens. The view is the exact same as before launching the authorization page.

To see if it had worked, I have it set to Toast a message saying "Login to Twitter Successful", in either onResume or onNewIntent (shown below), which never pops. The successful callback URL is received, however, as this entry shows up in LogCat:

12-18 09:25:50.426: I/ActivityManager(186): Starting: Intent { act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=snapp://twitter?oauth_token=tokenhere&oauth_verifier=verifierhere cmp=com.infini_servers.snapp/.SnappActivity } from pid 7853

Here's my onNewIntent (also have a virtual clone for onResume):

@Override
    protected void onNewIntent(Intent intent) 
    {
        super.onNewIntent(intent);        

        Uri uri = intent.getData();
        if (uri != null && uri.toString().startsWith(CALLBACKURL))
        {
            Toast.makeText(getBaseContext(), "Login to twitter successful!", Toast.LENGTH_LONG);
            String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
            try
            {
                provider.retrieveAccessToken(consumer, verifier); 

                AccessToken accessToken = new AccessToken(consumer.getToken(),
                        consumer.getTokenSecret());
                twitter.setOAuthConsumer(consumerKey, consumerSecret);
                twitter.setOAuthAccessToken(accessToken);
                String tweet = "Test";
                twitter.updateStatus(tweet);
                Toast.makeText(getBaseContext(), "Tweet Successful!", Toast.LENGTH_LONG);
            }
            catch (Exception e)
            {
                Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG);
            }
        }       
    }

And the relevant bits of my Manifest:

 <activity
            android:label="@string/app_name"
            android:name=".SnappLaunch" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            </activity>
        <activity
            android:label="@string/app_name"
            android:name=".SnappActivity"
            android:launchMode="singleInstance" >
            <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="snapp" android:host="twitter" /> 
            </intent-filter>
        </activity>

Toast.makeText(getBaseContext(), "Login to twitter successful!", Toast.LENGTH_LONG);

当您想烤面包时,别忘了调用“ show()”。

your onResume() method code put into inside of asynctask. its working fine.

see that sample,

class TwitterLogin extends AsyncTask<String, String, String> 
    {

        @Override
        protected String doInBackground(String... params) 
        {
            // TODO Auto-generated method stub

            Uri uri = getIntent().getData();                
            if (uri != null && uri.toString().startsWith(TWITTER_CALLBACK_URL))
            {
                String verifier = uri.getQueryParameter(URL_TWITTER_OAUTH_VERIFIER);
                try 
                {
                    AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, verifier);
                    // Shared Preferences
                    Editor e = loginPrefs.edit();
                    e.putString(PREF_KEY_OAUTH_TOKEN, accessToken.getToken());
                    e.putString(PREF_KEY_OAUTH_SECRET,accessToken.getTokenSecret());
                    e.putBoolean(PREF_KEY_TWITTER_LOGIN, true);
                    e.commit();

                    Log.e("Twitter OAuth Token", "> " + accessToken.getToken());


                    long userID = accessToken.getUserId();
                    User user = twitter.showUser(userID);
                    String username = user.getName();
                    Log.e("UserID: ", "userID: "+userID+""+username);

                    Log.v("Welcome:","Thanks:"+Html.fromHtml("<b>Welcome " + username + "</b>"));
                } 
                catch (Exception e) 
                {
                    Log.e("Twitter Login Error", "> " + e.getMessage());
                }
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) 
        {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

        }

        @Override
        protected void onPreExecute() 
        {
            // TODO Auto-generated method stub
            super.onPreExecute();

        }
    }

I've recently been playing with Twitter4j and had some problems that sound similar to those you are experiencing. I can't see anything wrong in the code you've posted, but I wonder what the callback URL you're passing to getOauthRequestToken is? I had some issues with this myself. I think your CALLBACKURL needs to be set to "snapp://twitter". Note that you need the colon.

If that doesn't work, I suggest you try removing the android:host from the manifest line:

<data android:scheme="snapp" android:host="twitter" />

so you're left with:

<data android:scheme="snapp" />

and then in the code where you make the call to getOauthRequestToken, pass the value "snapp:///". This was basically what I did to first get it working.

If this then works, you could try experimenting with putting the android:host="twitter" back and then changing the value passed to getOauthRequestToken.

If this doesn't work, have you seen if the onNewIntent is called at all - ie is it just not passing the uri.toString().startsWith(CALLBACKURL) test as your toast is only displayed if that passes? You could try putting some logging in or add a break point in the debugger.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM