2

我正在尝试将 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 网站上完成身份验证后,它应该将我重定向回应用程序。

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

替代文字

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

如何使用我找回的密钥返回我的应用程序?

4

3 回答 3

7

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

<intent-filter>不在应用程序中..

现在是这样的:

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

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

有没有办法在不重新启动整个应用程序的情况下“返回”到最后一个活动?

于 2010-12-08T13:18:23.367 回答
5

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

  1. 使用 webview 而不是在 web 浏览器中打开它:这样做的关键优势之一是,您可以跟踪 url 重定向。

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

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

  4. 完成任务后,您可以隐藏活动或删除 webView 或任何您想要的东西。

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

希望它会帮助你。

于 2010-12-10T05:17:06.620 回答
0

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

private final String CALLBACKURL = "sosInternational://HierBenIkNu";
于 2010-12-10T02:16:44.400 回答