Tuesday 30 September 2014

The evil "QWZV...==" in PayPal Oauth2.0 for user profile sharing

OK, so today I was ready to fetch some user profile information from PayPal account and I bump into a problem, PayPal is using OAuth2.0 to grant user access token which can be used to fetch all relevant informations that has been exposed by the PayPal application that you connected with.

When I was reading the PayPal official guide, I saw this.

OK, here are the 2 sections that has been highlighted.
1. the production link
It took me quite while to figure out that this is actually the production link that I'm testing with! The correct link should come from sandbox which is

https://api.sandbox.paypal.com/v1/oauth2/token

The guide should have indicated it, right? LOL.

2. The evil QWZV...==
the funny thing is the document never tells you where does this value come from. After a bit research, this is actually Basic Http Authentication headers and according to this https://developer.paypal.com/webapps/developer/docs/integration/direct/make-your-first-call/#get-an-access-token, this value should be generated by combining client_id and secret with ":" separated by.

        String s = BASE64Encoder.encode("yout_client_id:your_client_secret".getBytes());

Here is how you get that value in Java FYI.

We are finally good to go to get the access token.




I hope this solves your problem with getting access token from PayPal, happy integration with PayPal, :)

Thursday 25 September 2014

Safely delete fragment in viewpager

People tend to have multiple fragments when using viewpager, here is a good way to get rid of those fragments after you exit the current screen that has the viewpager.

in your onDestroy method:



  
  for (int i = 0; i < pagerCount; i++) {
       FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
       MyFragment f = (MyFragment) getSupportFragmentManager().findFragmentByTag(getFragmentTag(i));
       trans.remove(f).commitAllowingStateLoss();

  }
private String getFragmentTag(int position) {
        return "android:switcher:" + R.id.your_view_pageer_id + ":" + position;
    }
Hope this helps :)

Sunday 3 March 2013

Android Twitter integration. twitter4j How to logout from twitter4j

After working with twitter4j for a while I came to a point where user needs to logout from twitter account and let another user to login. Searching google doesn't seem to help a lot as there is no best solution out there I supposed. So I have created a work around for it and it seems to work reasonably well.

1. Some important parameter that needs to append to the url
Accroding to the twitter API reference:

force_login and screen_name are the 2 parameters that can be added to the OAuth token request. This means when user opens the webview in Android application, it will display the login page with username blank if you set the screen_name equals to empty. This is important as when you switch the user account you don't really want the next user to see the previous user's name.

2. Cancel the twitter login webview(logout)
Sometimes user wants to click cancel (as back button) on webview , but the Twitter instance has already be instantiated and if you re-use it it will throw exception saying Access token already exists(as I can remembner :)), to resolve this apply the following code into you activity's onActivityResult method:




  
if(resultCode == Activity.RESULT_CANCELED){
mTwitter.shutdown();
  mTwitter = new TwitterFactory().getInstance();
  mTwitter.setOAuthConsumer(getResources().getString(R.string.twitter_consumer_key),
    (getResources().getString(R.string.twitter_consumer_secret)));
}
By doing this, the twitter instance will be reset and ready for next user to user it.


To log out the user simply call the following method:

mTwitter.setOAuthAccessToken(null);
mTwitter.shutdown();
These may not be the best solution so far as I know, but at least it does the trick, please drop me a line if you find anything better or. Thanks!

Tuesday 26 February 2013

Android facebook SDK throwing exception : remote_app_id does not match stored id




Recently I was trying to make Facebook SDK integrated with the native android app. It was quite tricky to get it working and you may come across this exception:

ApiException:  remote_app_id does not match stored id






  • First thing you will need to do is to put the KeyHash in your App configuration in your Facebook developer dashboard.

  • As showon here :
    To get the KeyHash from keytool:
    keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%\.android\debug.keystore | openssl sha1 -binary | openssl base64
    
    it will ask for the password, type "android" (without quotes)

    Finally paste the generated hash code the your dashboard and click "save changes".


    • Sometimes the openssl installed on your computer may produce a different key hash code for your andoird application, and most likely it will throws ApiException :  remote_app_id does not match stored id . To resolve this check the hashcode generated from the code:

            try {
                PackageInfo info = getPackageManager().getPackageInfo(
                        "com.example.yourpackagename", 
                        PackageManager.GET_SIGNATURES);
                for (Signature signature : info.signatures) {
                    MessageDigest md = MessageDigest.getInstance("SHA");
                    md.update(signature.toByteArray());
                    Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT));
                    }
            } catch (NameNotFoundException e) {
    
            } catch (NoSuchAlgorithmException e) {
    
            }
    
    • Remember to use the correct package name as you put in your facebook app dashboard, you can also abtain it from the AndroidManifest.xml, "package=com.example.yourpackagename"
    open command prompt and type in "adb logcat" because the facebook exception may not be displayed in the IDE's logcat. Observe the KeyHash generated from the above code. It may be different from what you did by using keytool, copy and paste it to you dashboard, save the chages. Wait for the facebook to update its server and thats it! You should now be able to Login to facebook, calling APIs from you own app!