如何正确使用AccessGrant连接到android socialauth提供程序

时间:2014-03-23 00:16:50

标签: android android-asynctask socialauth

我的Android应用程序存储了成功的socialauth授权的android socialauth AccessGrant对象。我将它保存在sqlite中并且我试图使用存储的AccessGrant对象来允许具有AsycTask的服务连接到提供者(facebook,googleplus等),而不向用户显示登录屏幕。 这是服务的AsyncTask中的代码段...

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

这段代码给了我一个" oauth_consumer.properties文件在你的类路径中找不到"错误...然后我将oauth_consumer.properties添加到build.gradle文件

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

无论我尝试使用AuthManager或AuthProviderFactory的哪种方式,现在只会产生NullPointerException。

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)

如果那里有任何android socialauth专家,请你告诉我:
- 我在哪里错误地实施了这个? - 我应该寻找另一种方法来实现这个目标吗? - 为什么当我尝试直接使用AuthManager或AuthProviderFactory时找不到oauth_consumer.properties文件,但是在Activity中使用AuthAdapter时会发现它(根据socialauth andriod示例项目)?

谢谢!

1 个答案:

答案 0 :(得分:1)

解决! 这绝对是我的错。

由于某些原因,找不到属性文件,所以这里是我解决其他人陷入困境的解决方案:

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

然后在AsyncTask中停下来......

                provider = manager.connect(currentAccessGrant);

                provider.registerPlugins();

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