1

My android app is storing the android socialauth AccessGrant object from a successful socialauth authorization. I'm persisting it in sqlite and am attempting to used that stored AccessGrant object to allow a Service with an AsycTask connect to a provider (facebook, googleplus, etc.) without displaying login screens to the user. Here's the segment of code from within the AsyncTask of the Service...

AccessGrant currentAccessGrant = new AccessGrant(cn.getKey(),cn.getSecret());

currentAccessGrant.setProviderId(cn.getProviderId());
currentAccessGrant.setAttributes(cn.getAttributes());

manager = new SocialAuthManager();

try {                        
  //manager.getSocialAuthConfig().load("C:\\Users\\Blair\\Documents\\GitHub\\App3\\app3\\src\\main\\assets\\oauth_consumer.properties");
  //manager.getSocialAuthConfig().load();
  manager.getSocialAuthConfig();

} catch(Exception e) {
  Log.e(DEBUG_TAG, "SocialAuthConfig.load Exception", e);
}

if (cn.getProviderId().equals("googleplus")) {
  Log.d(DEBUG_TAG, "Setting googleplus");
  //currentAdapter.addProvider(SocialAuthAdapter.Provider.GOOGLEPLUS, R.drawable.googleplus);

  try {
    //provider = AuthProviderFactory.getInstance(currentAccessGrant.getProviderId(), oauth_consumer.properties);
    //provider = AuthProviderFactory.getInstance(currentAccessGrant.getProviderId());
    //provider = AuthProviderFactory.getInstance("googleplus");

    provider = manager.connect(currentAccessGrant);

    //currentAdapter.getCurrentProvider().setAccessGrant(currentAccessGrant);

  } catch(Exception e) {
    Log.e(DEBUG_TAG, "Social AuthProviderFactory Exception",e);
  }
}

This code gave me a "oauth_consumer.properties file is not found in your class path" error...then I added oauth_consumer.properties to the build.gradle file

dependencies {
    compile 'com.android.support:appcompat-v7:+'
    compile files('libs/socialauth-android-3.0.jar')
    compile files('libs/socialauth-4.3.jar')
    compile files ('src/main/assets/oauth_consumer.properties')
}

This now just produces a NullPointerException no matter which way I attempt to use the AuthManager or AuthProviderFactory.

03-22 18:06:31.834  29178-29356/com.myapp.app3 E/DownloadFriendService$DownloaderTask﹕ SocialAuthConfig.load Exception
    java.lang.NullPointerException
            at com.myapp.app3.service.DownloadFriendService$DownloaderTask.doSomething(DownloadFriendService.java:127)
            at com.myapp.app3.service.DownloadFriendService$DownloaderTask.doInBackground(DownloadFriendService.java:87)
            at com.myapp.app3.service.DownloadFriendService$DownloaderTask.doInBackground(DownloadFriendService.java:77)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
03-22 18:06:31.834  29178-29356/com.myapp.app3 D/DownloadFriendService$DownloaderTask﹕ Setting googleplus
03-22 18:06:31.834  29178-29356/com.myapp.app3 E/DownloadFriendService$DownloaderTask﹕ Social AuthProviderFactory Exception
    java.lang.NullPointerException
            at org.brickred.socialauth.SocialAuthManager.getProviderInstance(SocialAuthManager.java:317)
            at org.brickred.socialauth.SocialAuthManager.connect(SocialAuthManager.java:257)
            at com.myapp.app3.service.DownloadFriendService$DownloaderTask.doSomething(DownloadFriendService.java:163)
            at com.myapp.app3.service.DownloadFriendService$DownloaderTask.doInBackground(DownloadFriendService.java:87)
            at com.myapp.app3.service.DownloadFriendService$DownloaderTask.doInBackground(DownloadFriendService.java:77)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)

If there are any android socialauth experts out there, can you please tell me:
- where am I implementing this incorrectly?
- should I be looking for another way of achieving this?
- why the oauth_consumer.properties file isn't found when I try to use the AuthManager or AuthProviderFactory directly, but it is found when using the AuthAdapter in an Activity (as per the socialauth andriod example projects)?

Thanks!

user3188040
  • 671
  • 9
  • 24

1 Answers1

1

Solved! It definitely was my fault.

For some reason the properties file was not being found, so here's my solution for others who get stuck in the same place:

onStartCommand....

config = SocialAuthConfig.getDefault();

try {
    config.load("assets/oauth_consumer.properties");
} catch(Exception e) {
    Log.e(DEBUG_TAG, "SocialAuthConfig.load 1 Exception", e);
}


manager = new SocialAuthManager();

try {
    manager.setSocialAuthConfig(config);
} catch(Exception e) {
    Log.e(DEBUG_TAG, "SocialAuthManager.setSocialAuthConfig 1 Exception", e);
}

then down in the AsyncTask....

                provider = manager.connect(currentAccessGrant);

                provider.registerPlugins();

                try {
                    contacts = provider.getContactList();
                } catch (Exception e) {
                    Log.e(DEBUG_TAG, "Possible expired token?/n", e);
                }
user3188040
  • 671
  • 9
  • 24