Android Twitter 集成 - 卡在登录页面?

标签 android twitter android-webview android-twitter

我有来自以前的 Android 应用程序的代码,我已成功将其与 Twitter 集成。我已将此代码复制到一个新应用并更改了我的新应用的 callback-urlconsumer-keyconsumer-secret .

使用 twitter4j 库,我可以获得我的 RequestToken 和身份验证 url,如下所示:

Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(myConsumerKey, myConsumerSecret);
RequestToken requestToken = twitter.getOAuthRequestToken(myCallbackUrl);
String authenticationUrl = requestToken.getAuthenticationURL()

RequestToken 有一个非空的 token 值、一个非空的 tokenSecret 值和一个空的 secretKeySpec值。认证url格式如下:

http://api.twitter.com/oauth/authenticate?oauth_token=...

我在我的 WebView 中加载了这个 url,我看到了以下页面:

Sign in page

我卡在这里了。当我单击 Sign In 按钮时,同一页面会不断加载。没有其他的。但是,当我单击 Cancel 按钮时,我会看到以下页面:

enter image description here

只有在这个页面上,当我点击 Return to Fan League Beko BBL 按钮时,我的 callback-url 才会被调用,带有一个 denied 参数它的值是我的 oauth_token。任何人以前见过这样的事情并且知道什么可能阻止我的登录请求被处理???

Update 1: I've tried the authentication url and the Sign In button from a desktop browser and it works as expected, processing the response and then invoking the callback-url. It's just failing when trying it in the WebView of my Android app. I've tried it on multiple Android devices and tablets. JavaScript is enabled on my WebView too so that's not it. Ideas most welcome. I'm out of ideas!

Update 2: I've just done some debug-mode code-tracing on my WebView's WebViewClient and I'm seeing that when I click the Sign In button, the shouldOverrideUrlLoading(WebView webView, String url) method is not called, but the following three methods are called (in order): onPageStarted(WebView view, String url, Bitmap favicon), onLoadResource(WebView view, String url), onPageFinished(WebView view, String url). The url value these methods have is: https://api.twitter.com/oauth/authenticate, i.e. the oauth_token parameter has been stripped off the authenticate url. Maybe this is a POST request being treated as a GET request and hence why this one same page keeps loading up? Any ideas what I can do to have this Sign In button press processed properly?

最佳答案

我猜你忘了从 Twitter 应用程序控制面板设置回调 URL。

登录到 twitter api 部分,选择您的应用并转到设置选项卡。

enter image description here

如果不这样做,当用户按下登录时,将不会发生重定向,因此您将无法捕获验证器。

另一方面,当您设置回调 url 时,您的 webview 可以拦截重定向。

注意:您可以设置任何您想要的 url,重要的是要捕获传递给重定向 url 的 oauth_verifier 参数。

在那种情况下你的

shouldOverrideUrlLoading

应该被触发。

关于Android Twitter 集成 - 卡在登录页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14811556/

相关文章:

android - PWA 未安装为 WebApk

java - 将 Android Activity 结果委托(delegate)给其他 Activity

jquery - 单击它时像按钮一样动画

android - WebView 是否需要 WebViewClient 才能工作?

Phonegap 上的 Android Webview

android - 错误: Use of fullScreenIntent requires the USE_FULL_SCREEN_INTENT permission

android - 理解 "setComponentEnabledSettings"的 onEnabled/onDisabled 方法

filter - Twitter 流按位置过滤

iphone - iPhone OS4 的 Twitter API

android - WebView 的互联网使用情况(数据使用情况)