谷歌+;登录Android Studio-GoogleAuthException:BadUsername
登录成功后,我无法检索任何配置文件信息,并且在GoogleAuthUtil.getToken()方法上不断出现错误 错误:谷歌+;登录Android Studio-GoogleAuthException:BadUsername,android,android-studio,google-plus,Android,Android Studio,Google Plus,登录成功后,我无法检索任何配置文件信息,并且在GoogleAuthUtil.getToken()方法上不断出现错误 错误: com.google.android.gms.auth.GoogleAuthException: BadUsername Android上的客户端ID是根据Android包名和开发者控制台项目中签名密钥的SHA-1指纹的组合自动推断出来的。我匹配了包名,甚至尝试了工作站上的所有debug.keystore。什么都没用 *同意屏幕已设置(!),但未显示 整个工作片段: pu
com.google.android.gms.auth.GoogleAuthException: BadUsername
Android上的客户端ID是根据Android包名和开发者控制台项目中签名密钥的SHA-1指纹的组合自动推断出来的。我匹配了包名,甚至尝试了工作站上的所有debug.keystore。什么都没用
*同意屏幕已设置(!),但未显示
整个工作片段:
public class GooglePlusActivity extends Fragment implements ConnectionCallbacks, OnConnectionFailedListener {
private static final String TAG = "SignInActivity";
// magic number use to know that sign-in error
// resolution activity has completed.
private static final int REQUEST_RESOLVE_ERROR = 9000;
// core Google+ client.
private GoogleApiClient mGoogleApiClient;
// flag to stop multiple dialogues appearing for the user.
private boolean mResolvingError;
//TOKEN
protected String mToken;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "Create");
Plus.PlusOptions options = new Plus.PlusOptions.Builder()
.addActivityTypes("http://schemas.google.com/AddActivity",
"http://schemas.google.com/ReviewActivity")
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this.getActivity())
.addApi(Plus.API, options)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
// use mResolveOnFail as a flag to say whether we should trigger
// the resolution of a connectionFailed ConnectionResult.
mResolvingError = false;
Log.i(TAG, "Signing in...");
}
@Override
public void onStart() {
super.onStart();
Log.i(TAG, "Start");
// Every time we start we want to try to connect. If it
// succeeds we'll get an onConnected() callback. If it
// fails we'll get onConnectionFailed(), with a result!
if (!mResolvingError)
{
mGoogleApiClient.connect();
}
}
@Override
public void onStop() {
super.onStop();
// It can be a little costly to keep the connection open
// to Google Play Services, so each time our activity is
// stopped we should disconnect.
Log.i(TAG, "Stop");
mGoogleApiClient.disconnect();
}
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "ConnectionFailed : " + result.toString());
if (mResolvingError) {
// Already attempting to resolve an error.
return;
} else if (result.hasResolution()) {
try {
mResolvingError = true;
result.startResolutionForResult(this.getActivity(), REQUEST_RESOLVE_ERROR);
} catch (SendIntentException e) {
// There was an error with the resolution intent. Try again.
mGoogleApiClient.connect();
}
} else {
// Show dialog using GooglePlayServicesUtil.getErrorDialog()
Log.i(TAG,"Error num- " + result.getErrorCode());
mResolvingError = true;
}
}
//String scope = "oauth2:" + Scopes.PLUS_LOGIN;
//Plus.AccountApi.getAccountName(mGoogleApiClient)
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Connected.");
// Turn off the flag, so if the user signs out they'll have to
// tap to sign in again.
mResolvingError = false;
if(mToken == null || mToken.isEmpty())
{
// Retrieve the oAuth 2.0 access token.
final Context context = this.getActivity();
final String scope = "oauth2:"+ Scopes.PLUS_LOGIN;
AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
String token = null;
try {
token = GoogleAuthUtil.getToken(
context,
Plus.AccountApi.getAccountName(mGoogleApiClient),
scope);
} catch (IOException transientEx) {
// Network or server error, try later
Log.e(TAG, transientEx.toString());
} catch (UserRecoverableAuthException e) {
// Recover (with e.getIntent())
Log.e(TAG, e.toString());
Intent recover = e.getIntent();
startActivityForResult(recover, REQUEST_RESOLVE_ERROR);
} catch (GoogleAuthException authEx) {
// The call is not ever expected to succeed
// assuming you have already verified that
// Google Play services is installed.
Log.e(TAG, authEx.toString());
}
return token;
}
@Override
protected void onPostExecute(String token) {
mToken = token;
Toast.makeText(context, "GoogleToken: "+ token, Toast.LENGTH_LONG).show();
Log.v(TAG, "Access token retrieved:" + token);
}
};
task.execute();
}
}
public void onDisconnected() {
Log.i(TAG, "Disconnected.");
}
public void onActivityResult(int requestCode, int responseCode, Intent intent) {
Log.i(TAG, "ActivityResult: " + requestCode);
if (requestCode == REQUEST_RESOLVE_ERROR) {
mResolvingError = false;
if (responseCode == GooglePlusActivity.CAUSE_SERVICE_DISCONNECTED) {
// Make sure the app is not already connected or attempting to connect
if (!mGoogleApiClient.isConnecting() && !mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
}
}
}
@Override
public void onConnectionSuspended(int arg0) {
// onConnectionSuspended
Log.i(TAG, "onConnectionSuspended.");
} }
公共类GooglePlusaActivity扩展片段实现了ConnectionCallbacks、OnConnectionFailedListener{
私有静态最终字符串标记=“符号活动”;
//幻数用于知道登录错误
//解决活动已完成。
私有静态最终整数请求\u解决\u错误=9000;
//核心谷歌+客户端。
私人GoogleapClient MGoogleapClient;
//停止为用户显示多个对话的标志。
私有布尔mResolvingError;
//代币
保护字符串mToken;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Log.i(标记“创建”);
Plus.PlusOptions=new Plus.PlusOptions.Builder()
.addActivityTypes(“http://schemas.google.com/AddActivity",
"http://schemas.google.com/ReviewActivity")
.build();
mGoogleApiClient=new-GoogleApiClient.Builder(this.getActivity())
.addApi(Plus.API,选项)
.addConnectionCallbacks(此)
.addOnConnectionFailedListener(此)
.build();
//使用mResolveOnFail作为标志来说明是否应该触发
//ConnectionResult解析失败的ConnectionResult。
mResolvingError=false;
Log.i(标记“登录…”);
}
@凌驾
public void onStart(){
super.onStart();
Log.i(标记“开始”);
//每次我们开始时,我们都想尝试连接。如果
//如果成功,我们将获得OnConnectiond()回调
//失败我们将获得onConnectionFailed(),结果是!
如果(!mResolvingError)
{
mGoogleApiClient.connect();
}
}
@凌驾
公共void onStop(){
super.onStop();
//保持连接打开可能会有点昂贵
//谷歌播放服务,所以每次我们的活动
//我们应该断开连接。
Log.i(标记“停止”);
mGoogleApiClient.disconnect();
}
@凌驾
连接失败的公共void(连接结果){
Log.i(标记“ConnectionFailed:+result.toString());
如果(mResolvingError){
//已尝试解决错误。
返回;
}else if(result.hasResolution()){
试一试{
mResolvingError=true;
result.startResolutionForResult(this.getActivity(),请求\u解决\u错误);
}捕获(发送){
//解析意图出错。请重试。
mGoogleApiClient.connect();
}
}否则{
//使用GooglePlayServicesUtil.getErrorDialog()显示对话框
Log.i(标记“Error num-”+result.getErrorCode());
mResolvingError=true;
}
}
//String scope=“oauth2:”+Scopes.PLUS\u登录;
//Plus.AccountApi.getAccountName(mGoogleApiClient)
@凌驾
未连接的公共空间(捆绑包){
Log.i(标记“已连接”);
//关闭标志,这样如果用户注销,他们就必须
//点击以再次登录。
mResolvingError=false;
if(mToken==null | | mToken.isEmpty())
{
//检索oAuth 2.0访问令牌。
final Context Context=this.getActivity();
最后一个字符串scope=“oauth2:”+Scopes.PLUS\u登录;
AsyncTask任务=新建AsyncTask(){
@凌驾
受保护字符串doInBackground(无效…参数){
字符串标记=null;
试一试{
token=GoogleAuthUtil.getToken(
上下文
Plus.AccountApi.getAccountName(mGoogleApiClient),
范围);
}捕获(IOException transientEx){
//网络或服务器错误,请稍后再试
Log.e(TAG,transientEx.toString());
}捕获(UserRecoverableAuthe异常){
//恢复(使用e.getIntent())
Log.e(标记,e.toString());
Intent recover=e.getIntent();
startActivityForResult(恢复、请求\u解决\u错误);
}捕获(GoogleAuthException authEx){
//这一呼吁永远不会成功
//假设你已经证实了这一点
//安装了Google Play服务。
Log.e(TAG,authEx.toString());
}
返回令牌;
}
@凌驾
受保护的void onPostExecute(字符串标记){
mToken=代币;
Toast.makeText(上下文,“GoogleToken:+标记,Toast.LENGTH_LONG).show();
Log.v(标记“检索到的访问令牌:”+令牌);
}
};
task.execute();
}
}
公共空间已断开连接(){
Log.i(标记“断开”);
}
ActivityResult上的公共无效(int请求代码、int响应代码、意图){
Log.i(标记“ActivityResult:+requestCode”);
if(requestCode==请求\解决\错误){
mResolvingError=false;
if(responseCode==GooglePlusActivity.CAUSE\u服务\u断开连接){
//确保应用程序尚未连接或正在尝试连接
如果(!mgoogleapclient.isConnecting()&&!mgoogleapclient.isConnected()){
mGoogleApiClient.connect();
}
}
}
}
@凌驾
连接上的公共无效已暂停(int arg0){
//onConnectionSuspended
Log.i(标签“onConnectionSuspended”);
} }
我做错了什么?您能从
Plus.Acc发布返回值吗
<uses-permission android:name="android.permission.GET_ACCOUNTS" />