我有来自以前的 Android 应用程序的代码,我已成功将其与 Twitter 集成。我已将此代码复制到一个新应用并更改了我的新应用的 callback-url
、consumer-key
和 consumer-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
按钮时,同一页面会不断加载。没有其他的。但是,当我单击 Cancel
按钮时,我会看到以下页面:
只有在这个页面上,当我点击 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 theWebView
of my Android app. I've tried it on multiple Android devices and tablets. JavaScript is enabled on myWebView
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
'sWebViewClient
and I'm seeing that when I click theSign In
button, theshouldOverrideUrlLoading(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)
. Theurl
value these methods have is:https://api.twitter.com/oauth/authenticate
, i.e. theoauth_token
parameter has been stripped off the authenticate url. Maybe this is aPOST
request being treated as aGET
request and hence why this one same page keeps loading up? Any ideas what I can do to have thisSign In
button press processed properly?
最佳答案
我猜你忘了从 Twitter 应用程序控制面板设置回调 URL。
登录到 twitter api 部分,选择您的应用并转到设置选项卡。
如果不这样做,当用户按下登录时,将不会发生重定向,因此您将无法捕获验证器。
另一方面,当您设置回调 url 时,您的 webview 可以拦截重定向。
注意:您可以设置任何您想要的 url,重要的是要捕获传递给重定向 url 的 oauth_verifier 参数。
在那种情况下你的
shouldOverrideUrlLoading
应该被触发。
关于Android Twitter 集成 - 卡在登录页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14811556/