微博认证后回调 [英] CallBack after Twitter authentication

查看:154
本文介绍了微博认证后回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想给Twitter集成到我的应用程序,但我似乎无法得到它的工作。

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

这是我的code:

public class OAuthForTwitter extends Activity {

    private CommonsHttpOAuthConsumer httpOauthConsumer;
    private OAuthProvider httpOauthprovider;
    public final static String consumerKey = "xxxxxxxxxxxxxx";
    public final static String consumerSecret = "xxxxxxxxxxxx";
    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:

我有这个在我的Andr​​oidManifest:

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?

推荐答案

好了,这是相当一个愚蠢的错误。

Ok, it was quite a dumb mistake.

我的&LT;意向滤光器&gt; 不是一个应用程序中。

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

这是它现在怎么是:

<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?

这篇关于微博认证后回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆