简体   繁体   English

Twitter4j OAuth回调成功,但测试鸣叫失败

[英]Twitter4j OAuth callback succeeds, but test tweet fails

Having a rough time with OAuth in Twitter4j. 在Twitter4j中使用OAuth时遇到困难。 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. 在此之前,我遇到了#3255153中详细描述的问题和401错误,但最终解决了这些问题,遇到了更难解决的问题。

The Twitter application authorization page launches in a browser, I log in, and approve the application for my account. Twitter应用程序授权页面在浏览器中启动,我登录并批准我的帐户的应用程序。 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. 要查看它是否起作用,我将其设置为Toast,其中包含永不弹出的onResume或onNewIntent(如下所示)中的消息“登录Twitter成功”。 The successful callback URL is received, however, as this entry shows up in LogCat: 但是,成功接收到回调URL,因为此条目显示在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): 这是我的onNewIntent(也具有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. 您的onResume()方法代码放入了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. 我最近一直在玩Twitter4j,遇到了一些听起来与您遇到的问题类似的问题。 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? 在您发布的代码中我看不到任何错误,但是我想知道您传递给getOauthRequestToken的回调URL是什么? I had some issues with this myself. 我自己对此有一些问题。 I think your CALLBACKURL needs to be set to "snapp://twitter". 我认为您的CALLBACKURL需要设置为“ 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: 如果这不起作用,建议您尝试从清单行中删除android:host:

<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:///". 然后在调用getOauthRequestToken的代码中,传递值“ 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. 如果这样可行,您可以尝试将android:host =“ twitter”放回去,然后更改传递给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? 如果这不起作用,您是否看到过onNewIntent是否被调用-即它只是不通过uri.toString()。startsWith(CALLBACKURL)测试,因为仅当您通过时才显示烤面包? You could try putting some logging in or add a break point in the debugger. 您可以尝试进行一些登录或在调试器中添加断点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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