已有类似的问题。但在我的情况下,我检查了SHA1和我的范围,它们是正确的,该帐户也可用。但它仍然会返回未知的GoogleAuthException。我的代码是: -
MainActivity.java
package com.simon.learn.accountgetter;
import java.io.IOException;
import java.util.ArrayList;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.auth.GoogleAuthException;
import com.google.android.gms.auth.GoogleAuthUtil;
import com.google.android.gms.auth.UserRecoverableAuthException;
import com.google.android.gms.common.AccountPicker;
public class MainActivity extends Activity {
AccountManager am;
Account[] accounts;
ArrayList<String> accNames = new ArrayList<String>();
Button signin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
am = AccountManager.get(getApplicationContext());
accounts = am.getAccountsByType("com.google");
// accounts=am.getAccounts();
signin = (Button) findViewById(R.id.signinBt);
signin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = AccountPicker.newChooseAccountIntent(null,
null, new String[] { "com.google" }, false, null, null,
null, null);
startActivityForResult(intent, 13);
}
});
}
private void getToken(String accountName) {
new GetTokenTask(MainActivity.this, accountName,
"oauth2:https://www.googleapis.com/auth/userinfo.profile")
.execute();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String token = null;
if (requestCode == 13) {
if (resultCode == RESULT_OK) {
String accountName = data
.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
Log.e("simon", accountName);
Toast.makeText(getApplicationContext(), accountName,
Toast.LENGTH_LONG).show();
getToken(accountName);
// Toast.makeText(getApplicationContext(),
// "The token is : "+token, Toast.LENGTH_LONG).show();
}
}
}
public class GetTokenTask extends AsyncTask<Void, Void, String> {
Activity activity;
String scope;
String email;
public GetTokenTask(Activity activity, String email, String scope) {
this.activity = activity;
this.scope = scope;
this.email = email;
}
@Override
protected String doInBackground(Void... params) {
String token = null;
try {
Log.e("simon","Scope : "+scope+ " email "+ email);
token = GoogleAuthUtil.getToken(activity, email, scope);
} catch (UserRecoverableAuthException e) {
// TODO Auto-generated catch block
Log.e("simon","UserRecoverableAuthException");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("simon","IOException");
e.printStackTrace();
} catch (GoogleAuthException e) {
// TODO Auto-generated catch block
Log.e("simon","GoogleAuthException : "+e.getMessage());
e.printStackTrace();
}
return token;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
Log.e("simon", "The Token is : " + result);
Toast.makeText(getApplicationContext(), "The Token is : " + result,
Toast.LENGTH_LONG).show();
super.onPostExecute(result);
}
}
}
显示未知的GoogleAuthException。请参阅我的清单文件
Manifest.java
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.simon.learn.accountgetter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
请帮帮我。提前致谢