WebView could not load oAuth url

I tried the codes from Android Twitter oAuth Connect Tutorial and it worked successfully. I tried changing the twitter authorization page to run in WebView instead of a web browser, but WebView did not seem to be able to load the url with this format oauth://twittersample , which is a link to my application. After successful authorization, the webview should close and return to my application.

An error occurred: "The webpage in oauth: // twittersample? Oauth_token = .... may be temporarily unavailable or may have moved to a new web address." What should I do?

This is a snippet of my webview that is in my 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("oauth://twittersample")) //handleTwitterCallback(url); { System.out.println("TWEET TWEET TWEET"); webView.loadUrl(url); return true; } else return false; } }); 

This is a link to my java class for Twitter TWITTER CONNECT CLASS And this is my manifest

 <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> 

Attached logcat file on successful launch in browser

enter image description here

+4
source share
3 answers

I finally earned it. I think this did not work before because I did not get the access token in WebView.

In my webview in onCreate I did it

 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; } }); 

and in my retrieveAccessToken (url) I have this.

 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()); } } 

I got this to work exactly the way I wanted it and successfully logged into the system. Correct me if I do something wrong.

Thanks @ user1690588 and @Nikolay Elenkov for your time to help me. :)

+4
source

Use an HTTP url like http://localhost/twittersample/oauth_callback or similar.

+1
source

I also had this problem and I understand that my callback url does not match my filter entry in my manifest file. You may also have the same problem.

You can solve this problem by following these steps:

set your callback url in your class as

 oauth://twittersample 

and in your AndroidManifest

 <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> 

Try ..,.

Make sure the callback url

 XXX://YYY 

corresponds to

 <data android:scheme="XXX" android:host="YYY"/> 

EDIT ................................................. .................................

Try it like also ..,.

0
source

All Articles