CallBack after Twitter authentication

◇◆丶佛笑我妖孽 提交于 2019-12-19 10:29:13

问题


I'm trying to integrate twitter to my app, but I can't seem to get it to work.

This is my code:

public class OAuthForTwitter extends Activity {

    private CommonsHttpOAuthConsumer httpOauthConsumer;
    private OAuthProvider httpOauthprovider;
    public final static String consumerKey = "{removed}";
    public final static String consumerSecret = "{removed}";
    private final String CALLBACKURL = "sosInternational:///HierBenIkNu";
    private Twitter twitter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        doOAuth();
    }

    /**
     * Opens the browser using signpost jar with application specific
     * consumerkey and consumerSecret.
     */

    private void doOAuth() {
        try {
            httpOauthConsumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret);
            httpOauthprovider = new DefaultOAuthProvider(
                    "http://twitter.com/oauth/request_token",
                    "http://twitter.com/oauth/access_token",
                    "http://twitter.com/oauth/authorize");
            String authUrl = httpOauthprovider.retrieveRequestToken(httpOauthConsumer, CALLBACKURL);
            this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
        } catch (Exception e) {
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {

        super.onNewIntent(intent);

        Uri uri = intent.getData();
        if (uri != null && uri.toString().startsWith(CALLBACKURL)) {

            String verifier = uri
                    .getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);

            try {
                // this will populate token and token_secret in consumer

                httpOauthprovider.retrieveAccessToken(httpOauthConsumer,
                        verifier);

                // TODO: you might want to store token and token_secret in you
                // app settings!!!!!!!!

                AccessToken a = new AccessToken(httpOauthConsumer.getToken(),
                        httpOauthConsumer.getTokenSecret());

                // initialize Twitter4J

                twitter = new TwitterFactory().getInstance();
                twitter.setOAuthConsumer(consumerKey, consumerSecret);
                twitter.setOAuthAccessToken(a);

                // create a tweet

                Date d = new Date(System.currentTimeMillis());
                String tweet = "#OAuth working! " + d.toLocaleString();

                // send the tweet

                twitter.updateStatus(tweet);

            } catch (Exception e) {

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

        }
    }
}

When I'm done authenticating on the Twitter site, it should redirect me back to the app.

But instead, I get this Page not found:

I have this in my AndroidManifest:

<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="HierBenIkNu"></data>  
    </intent-filter>  

How can I go back to my app with the keys i get back?


回答1:


Ok, it was quite a dumb mistake.

My <intent-filter> wasn't inside an application..

This is how it is now:

<activity 
        android:name=".OAuthForTwitter"
        android:label="@string/app_name"
        android:configChanges="orientation|keyboardHidden"
        android:launchMode="singleInstance">
        <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="OAuthForTwitter"></data>  
        </intent-filter>
    </activity>

This kind off works, it just loads the whole app from start.

Isn't there a way to just 'go back' to the last activity without restarting the whole app?




回答2:


I have solved this. Not exactly the way you have developed, but a slight different way. Here are the steps describing what i did.

  1. Use webview instead of opening it in web browser: One of the key advantage doing it is , you can track the url redirects.

  2. call setWebViewClient method of your webview and override shouldOverrideUrlLoading method of your class, i have used inner class.

  3. You will have url parameter in your method. Check whether it starts with your own call back url or not, (Note: This url contains User Token and user secret that is necessary for authorization).

  4. After you finish your task, you can hide the activity or remove the webView or any thing you desire.

EDIT : This is the oAuth way usually used in web application, so we don't need xAuth way. (In case other community members don't know)

Hope it will help you.




回答3:


Your callback URL should be "sosInternational://HierBenIkNu" (instead of "sosInternational:///HierBenIkNu") in the Java code.

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


来源:https://stackoverflow.com/questions/4387631/callback-after-twitter-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!