Twitter认证后回调

发布于 2024-10-06 07:39:59 字数 3501 浏览 6 评论 0原文

我正在尝试将 Twitter 集成到我的应用程序中,但我似乎无法让它工作。

这是我的代码:

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();
            }

        }
    }
}

当我在 Twitter 网站上完成身份验证后,它应该将我重定向回应用程序。

但相反,我发现找不到此页面:

alt text

我的 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>  

我怎样才能返回到我的应用程序与我拿回来的钥匙?

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:

alt text

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生来就爱笑 2024-10-13 07:39:59

好吧,这是一个非常愚蠢的错误。

我的 不在应用程序内。

现在就是这样:

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

这种关闭方式有效,它只是从一开始就加载整个应用程序。

是否有一种方法可以“返回”到上一个活动而无需重新启动整个应用程序?

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?

疑心病 2024-10-13 07:39:59

我已经解决了这个问题。与您开发的方式不完全相同,但略有不同。以下是描述我所做的步骤。

  1. 使用 webview 而不是在网络浏览器中打开它:这样做的主要优点之一是,您可以跟踪 url 重定向。

    使用

  2. 调用你的webview的setWebViewClient方法并覆盖你的类的shouldOverrideUrlLoading方法,我使用了内部类。

  3. 您的方法中将包含 url 参数。检查它是否以您自己的回调 url 开头(注意:此 url 包含授权所需的用户令牌和用户密钥)。

  4. 完成任务后,您可以隐藏 Activity 或删除 webView 或任何您想要的内容。

编辑:这是Web应用程序中通常使用的oAuth方式,因此我们不需要xAuth方式。 (以防其他社区成员不知道)

希望对您有所帮助。

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.

苍风燃霜 2024-10-13 07:39:59

您的回调 URL 在 Java 代码中应为“sosInternational://HierBenIkNu”(而不是“sosInternational:///HierBenIkNu”)。

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

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

private final String CALLBACKURL = "sosInternational://HierBenIkNu";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文