Unrecoverable exception while generating token using GoogleAuthUtil

  • Replies:0
sneha bansal
  • Forum posts: 1

Aug 28, 2013, 2:39:36 PM via Website

I want to use google authentication for login in my application .


My code :


public class MainActivity extends Activity {
String scope = "audience:server:client_id:SERVER CLIENT KEY";
int MY_ACTIVITYS_AUTH_REQUEST_CODE = 200;@
Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new getToken().execute(null, null, null);

}

@
Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_ACTIVITYS_AUTH_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
new getToken().execute(null, null, null);
}
}
}
public class getToken extends AsyncTask < Void, Void, Void > {

@Override
protected Void doInBackground(Void...params) {
// TODO Auto-generated method stub
getAndUseAuthTokenBlocking();

return null;
}

}
// Example of how to use the GoogleAuthUtil in a blocking, non-main thread context
void getAndUseAuthTokenBlocking() {

// Retrieve a token for the given account and scope. It will always return either
// a non-empty String or throw an exception.
String[] account = getAccountNames();
for (int i = 0; i < account.length; i++) {
try {
final String token = GoogleAuthUtil.getToken(MainActivity.this, account[i], "oauth2:" + Scopes.PLUS_PROFILE);
Log.e("token", token);
// Do work with token.
// if (server indicates token is invalid) {
// // invalidate the token that we found is bad so that GoogleAuthUtil won't
// // return it next time (it may have cached it)
// GoogleAuthUtil.invalidateToken(Context, String)(context, token);
// // consider retrying getAndUseTokenBlocking() once more
// return;
// }
return;
} catch (GooglePlayServicesAvailabilityException playEx) {
Dialog alert = GooglePlayServicesUtil.getErrorDialog(
playEx.getConnectionStatusCode(),
this,
MY_ACTIVITYS_AUTH_REQUEST_CODE);

} catch (UserRecoverableAuthException userAuthEx) {
Log.e("UserRecoverableAuthException", "UserRecoverableAuthException");
userAuthEx.printStackTrace();
// Start the user recoverable action using the intent returned by
// getIntent()
MainActivity.this.startActivityForResult(
userAuthEx.getIntent(),
MY_ACTIVITYS_AUTH_REQUEST_CODE);
return;
} catch (IOException transientEx) {
Log.e("IOException", "IOException");
transientEx.printStackTrace();

// network or server error, the call is expected to succeed if you try again later.
// Don't attempt to call again immediately - the request is likely to
// fail, you'll hit quotas or back-off.
return;
} catch (GoogleAuthException authEx) {
Log.e("GoogleAuthException", "GoogleAuthexception");
authEx.printStackTrace();

// Failure. The call is not expected to ever succeed so it should not be
// retried.
return;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}


}
private String[] getAccountNames() {
AccountManager mAccountManager = AccountManager.get(this);
Account[] accounts = mAccountManager.getAccountsByType(
GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
String[] names = new String[accounts.length];
for (int i = 0; i < names.length; i++) {
names[i] = accounts[i].name;
}
return names;
}
}

Now , when I put scope value
String scope = "audience:server:client_id:SERVER CLIENT KEY";
giving Exception UnknownResource

And when putting value for scope
String scope = Scopes.PLUS_PROFILE;
giving exception
08-28 14:02:07.228: E/UserRecoverableAuthException(886): UserRecoverableAuthException
08-28 14:02:07.238: W/System.err(886): com.google.android.gms.auth.UserRecoverableAuthException: NeedPermission
08-28 14:02:07.238: W/System.err(886): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
08-28 14:02:07.238: W/System.err(886): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
08-28 14:02:07.238: W/System.err(886): at com.android.mytokengenerator.MainActivity.getAndUseAuthTokenBlocking(MainActivity.java:65)
08-28 14:02:07.238: W/System.err(886): at com.android.mytokengenerator.MainActivity$getToken.doInBackground(MainActivity.java:51)
08-28 14:02:07.238: W/System.err(886): at com.android.mytokengenerator.MainActivity$getToken.doInBackground(MainActivity.java:1)
08-28 14:02:07.238: W/System.err(886): at android.os.AsyncTask$2.call(AsyncTask.java:264)
08-28 14:02:07.238: W/System.err(886): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-28 14:02:07.238: W/System.err(886): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-28 14:02:07.238: W/System.err(886): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-28 14:02:07.238: W/System.err(886): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-28 14:02:07.238: W/System.err(886): at java.lang.Thread.run(Thread.java:864)
08-28 14:02:08.658: E/ActivityThread(886): Activity com.android.mytokengenerator.MainActivity has leaked ServiceConnection com.google.android.gms.internal.h@40e47e20 that was originally bound here
08-28 14:02:08.658: E/ActivityThread(886): android.app.ServiceConnectionLeaked: Activity com.android.mytokengenerator.MainActivity has leaked ServiceConnection com.google.android.gms.internal.h@40e47e20 that was originally bound here
08-28 14:02:08.658: E/ActivityThread(886): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:993)
08-28 14:02:08.658: E/ActivityThread(886): at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:887)
08-28 14:02:08.658: E/ActivityThread(886): at android.app.ContextImpl.bindService(ContextImpl.java:1388)
08-28 14:02:08.658: E/ActivityThread(886): at android.content.ContextWrapper.bindService(ContextWrapper.java:370)
08-28 14:02:08.658: E/ActivityThread(886): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
08-28 14:02:08.658: E/ActivityThread(886): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
08-28 14:02:08.658: E/ActivityThread(886): at com.android.mytokengenerator.MainActivity.getAndUseAuthTokenBlocking(MainActivity.java:65)
08-28 14:02:08.658: E/ActivityThread(886): at com.android.mytokengenerator.MainActivity$getToken.doInBackground(MainActivity.java:51)
08-28 14:02:08.658: E/ActivityThread(886): at com.android.mytokengenerator.MainActivity$getToken.doInBackground(MainActivity.java:1)
08-28 14:02:08.658: E/ActivityThread(886): at android.os.AsyncTask$2.call(AsyncTask.java:264)
08-28 14:02:08.658: E/ActivityThread(886): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-28 14:02:08.658: E/ActivityThread(886): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-28 14:02:08.658: E/ActivityThread(886): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-28 14:02:08.658: E/ActivityThread(886): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-28 14:02:08.658: E/ActivityThread(886): at java.lang.Thread.run(Thread.java:864)
08-28 14:02:08.828: E/ActivityThread(886): Activity com.android.mytokengenerator.MainActivity has leaked ServiceConnection com.google.android.gms.internal.h@40e5fa58 that was originally bound here
08-28 14:02:08.828: E/ActivityThread(886): android.app.ServiceConnectionLeaked: Activity com.android.mytokengenerator.MainActivity has leaked ServiceConnection com.google.android.gms.internal.h@40e5fa58 that was originally bound here
08-28 14:02:08.828: E/ActivityThread(886): at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:993)

Not getting what is the problem.Do I need to use PlusClient Connectivity before generating token through GoogleAuthUtil.getToken ().Please help

Reply