android – WebView无法加载oAuth url
作者:互联网
我正在尝试Android Twitter oAuth Connect Tutorial的代码并且它成功运行.我试图将Twitter授权页面更改为在WebView而不是Web浏览器中运行,但WebView似乎无法使用此格式加载url:// twittersample这是返回到我的应用程序的链接.成功授权后,webview应关闭并成功返回我的应用程序.
有一个错误说“oauth:// twittersample?oauth_token = ….的网页可能暂时关闭,或者它可能已永久移动到新的网址”.我该怎么办?
这是我的onCreate中的WebView的片段
WebView myWebView = (WebView)findViewById(R.id.myWebView);
myWebView.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView webView, String url)
{
if (url != null && url.startsWith("oauth://twittersample"))
//handleTwitterCallback(url);
{
System.out.println("TWEET TWEET TWEET");
webView.loadUrl(url);
return true;
}
else
return false;
}
});
这是我的Twitter java类TWITTER CONNECT CLASS的链接
这是我的表现
<activity android:name="com.test.settings.ShareSettings" android:label="ShareSettings" android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden">
<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="oauth" android:host="twittersample"/>
</intent-filter>
</activity>
附件是在浏览器中成功运行时的logcat
解决方法:
我终于开始工作了.我认为之前它没有用,因为我没有将访问令牌检索到WebView中.
在onCreate下的我的WebView中,我做到了这一点
myWebView = (WebView)findViewById(R.id.myWebView);
myWebView.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView webView, String url)
{ if (url != null && url.startsWith(TWITTER_CALLBACK_URL))
{ System.out.println("TWEET TWEET TWEET");
retrieveAccessToken(url); //added this
webView.setVisibility(View.GONE); //added this
return true;
}
else
return false;
}
});
在我的retrieveAccessToken(url)中我有这个.
private void retrieveAccessToken(final String url)
{
Uri uri = Uri.parse(url);
String verifier = uri.getQueryParameter(URL_TWITTER_OAUTH_VERIFIER);
try {
// Get the access token
AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, verifier);
// Shared Preferences
Editor e = mSharedPreferences.edit();
// After getting access token, access token secret
// store them in application preferences
e.putString(PREF_KEY_OAUTH_TOKEN, accessToken.getToken());
e.putString(PREF_KEY_OAUTH_SECRET,accessToken.getTokenSecret());
// Store login status - true
e.putBoolean(PREF_KEY_TWITTER_LOGIN, true);
e.commit(); // save changes
Log.e("Twitter OAuth Token", "> " + accessToken.getToken());
TextView twitterConnect = (TextView) findViewById(R.id.twitterConnect);
String disconnect = "Disconnect";
twitterConnect.setText(disconnect);
// Getting user details from twitter
// For now i am getting his name only
long userID = accessToken.getUserId();
User user = twitter.showUser(userID);
String username = user.getName();
txtUpdate.setVisibility(View.VISIBLE);
btnUpdateStatus.setVisibility(View.VISIBLE);
// Displaying in xml ui
//twitterUser.setText(Html.fromHtml("<b>Welcome " + username + "</b>"));
TextView twitterUser = (TextView) findViewById(R.id.twitterDesc);
twitterUser.setText(Html.fromHtml(username));
Toast.makeText(getApplicationContext(), "LOGGED IN AS " + username, Toast.LENGTH_LONG).show();
} catch (Exception e) {
// Check log for login errors
Log.e("Twitter Login Error", "> " + e.getMessage());
}
}
我让这个工作正是我想要的,并成功登录.
如果我在这里做错了什么,请纠正我.
谢谢@ user1690588和@Nikolay Elenkov,您的时间帮助我.
标签:android,android-webview,android-twitter 来源: https://codeday.me/bug/20190620/1246618.html