Java 使用GoogleAuthUtil生成令牌时出现不可恢复的异常
我想在我的应用程序中使用google身份验证进行登录。为此,我遵循了以下链接 我的代码:Java 使用GoogleAuthUtil生成令牌时出现不可恢复的异常,java,android,android-intent,oauth-2.0,Java,Android,Android Intent,Oauth 2.0,我想在我的应用程序中使用google身份验证进行登录。为此,我遵循了以下链接 我的代码: package com.android.mytokengenerator; import java.io.IOException; import com.google.android.gms.auth.GoogleAuthException; import com.google.android.gms.auth.GoogleAuthUtil; import com.google.android.gms
package com.android.mytokengenerator;
import java.io.IOException;
import com.google.android.gms.auth.GoogleAuthException;
import com.google.android.gms.auth.GoogleAuthUtil;
import com.google.android.gms.auth.GooglePlayServicesAvailabilityException;
import com.google.android.gms.auth.UserRecoverableAuthException;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.Scopes;
import android.os.AsyncTask;
import android.os.Bundle;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
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;
}
}
现在,当我把范围值
字符串范围=访问群体:服务器:客户端\u id:服务器客户端密钥;
提供异常未知源
当为范围设定值时
字符串范围=Scopes.PLUS_PROFILE;
破例
无法获取问题所在。在通过GoogleAuthUtil.getToken生成令牌之前,是否需要使用PlusClient连接。请帮助