Twitter4j OAuth 回调成功,但测试推文失败
在 Twitter4j 中使用 OAuth 遇到了困难。在此之前,我遇到了 #3255153 中详细说明的问题和 401 错误,但最终修复了这些问题,并遇到了更难解决的问题问题。
Twitter 应用程序授权页面在浏览器中启动,我登录并批准我的帐户的应用程序。然后它重定向回应用程序,但什么也没有发生。该视图与启动授权页面之前完全相同。
为了查看它是否有效,我将其设置为在 onResume 或 onNewIntent(如下所示)中吐出一条消息,提示“登录 Twitter 成功”(如下所示),该消息永远不会弹出。但是,会收到成功的回调 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
这是我的 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);
}
}
}
以及我的 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>
Having a rough time with OAuth in Twitter4j. 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.
The Twitter application authorization page launches in a browser, I log in, and approve the application for my account. 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. The successful callback URL is received, however, as this entry shows up in 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):
@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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我最近一直在使用 Twitter4j,遇到了一些听起来与您遇到的问题类似的问题。我看不出您发布的代码有任何问题,但我想知道您传递给 getOauthRequestToken 的回调 URL 是什么?我自己对此也有一些问题。我认为您的 CALLBACKURL 需要设置为“snapp://twitter”。请注意,您需要冒号。
如果这不起作用,我建议您尝试从清单行中删除 android:host:
这样您就剩下:
然后在调用 getOauthRequestToken 的代码中,传递值“snapp:/// ”。这基本上就是我首先让它发挥作用所做的事情。
如果这有效,您可以尝试尝试将 android:host="twitter" 放回去,然后更改传递给 getOauthRequestToken 的值。
如果这不起作用,您是否看到 onNewIntent 是否被调用 - 即它是否只是没有通过 uri.toString().startsWith(CALLBACKURL) 测试,因为只有通过后才显示您的 toast?您可以尝试输入一些日志记录或在调试器中添加断点。
I've recently been playing with Twitter4j and had some problems that sound similar to those you are experiencing. 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? I had some issues with this myself. I think your CALLBACKURL needs to be set to "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:
so you're left with:
and then in the code where you make the call to getOauthRequestToken, pass the value "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.
If this doesn't work, have you seen if the onNewIntent is called at all - i.e. is it just not passing the uri.toString().startsWith(CALLBACKURL) test as your toast is only displayed if that passes? You could try putting some logging in or add a break point in the debugger.
当你想吐司时,不要忘记调用 'show()'。
Don't forget to call 'show()' when you want to make a toast.
您的 onResume() 方法代码放入 asynctask 内部。它工作正常。
看到那个样本,
your onResume() method code put into inside of asynctask. its working fine.
see that sample,