5

I was trying out codes from Android Twitter oAuth Connect Tutorial and it worked successfully. I tried to change the twitter authorization page to run in a WebView instead of a web browser but the WebView couldn't seem to load url with this format oauth://twittersample which is the link back to my application. Upon successful authorization, the webview should close and return to my app successfully.

There is an error saying "The web page at oauth://twittersample?oauth_token=.... might be temporarily down or it may have moved permanently to a new web address". What should I do?

This is the snippet to 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 the link to my Twitter java class 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 is the logcat when successfully run in browser

enter image description here

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Honey H
  • 299
  • 1
  • 6
  • 23
  • 1
    Please do not add "SOLVED" in your question titles. Selecting an answer as correct automatically shows it as solved in questions listing. – hjpotter92 May 14 '13 at 08:14

3 Answers3

5

I finally get it to work. I think earlier it did not work because I did not retrieve access token into the WebView.

In my WebView under onCreate i did this

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 how I wanted it and logged in successfully. Do correct me if I'm doing anything wrong here.

Thank you @user1690588 and @Nikolay Elenkov, for your time to help me out. :)

Honey H
  • 299
  • 1
  • 6
  • 23
1

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

Nikolay Elenkov
  • 52,576
  • 10
  • 84
  • 84
  • I tried your method, and it didn't work as well. I wonder what I did wrong. – Honey H Dec 03 '12 at 03:51
  • Did you update the intent filter in the manifest? What version of Android are you using? – Nikolay Elenkov Dec 03 '12 at 03:55
  • yeah I updated the intent filter in the manifest to match the url in my java class. I'm using Android 4.1.2. – Honey H Dec 03 '12 at 04:07
  • Hm, can you show more code? Does the initial URL you get work in a browser? BTW, this is a full tutorial that shows how to do this, it might be helpful to compare to your own approach: http://blog.doityourselfandroid.com/2011/08/08/improved-twitter-oauth-android/ – Nikolay Elenkov Dec 03 '12 at 04:29
  • Yeap, the initial URL worked well in a browser. Yeah I was looking on this tutorial as well. It is the same link posted by @user1690588 Okay hang on. Let me paste my java class. – Honey H Dec 03 '12 at 04:32
  • I just updated my manifest and my java class that contained twitter connect – Honey H Dec 03 '12 at 04:44
  • The only thing that jumps out is that you are enabling JavaScript after you load the URL. Those flows usually rely on JavaScript to do some parsing/redirecting, so that might have something to do with it. BTW, if you are doing everything inside your activity (via the WeView), you don't really need the intent filters, since control never leaves your app. – Nikolay Elenkov Dec 03 '12 at 05:13
  • I enabled javascript before I load the URL as you suggested. I still couldn't load the page successfully. Oh okay, thanks for telling me that. – Honey H Dec 03 '12 at 05:55
  • Does it work in the browser? Output in logcat and past in the device browser to verify. – Nikolay Elenkov Dec 03 '12 at 05:57
  • Yeap it worked well in the browser. It can load the Token successfully. I will upload the logcat above. Somehow it just don't work with the webview. – Honey H Dec 03 '12 at 07:30
  • Strange indeed. Other than loading the full sample project from the DYI Android blog and comparing line for line, not sure what else to suggest... – Nikolay Elenkov Dec 03 '12 at 07:35
  • 1
    It's okay. Thank you so much for your time by the way. Really appreciate it. :) I will continue trying and will post up the solution if I manage to figure it out. – Honey H Dec 03 '12 at 07:40
0

I was also having this problem and I realize that my callback url does not match with my filter entry in my manifest file. May be you also have same problem.

You can solve this by following 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 it..,.

Make sure your callback url

XXX://YYY 

match with

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

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

Try this like also..,.

MKB
  • 7,587
  • 9
  • 45
  • 71
  • Yeah I have that in my manifest too, and it matches what I have in my java class, but I still face the same problem. – Honey H Dec 02 '12 at 13:28