Sta*_*ids 3 authentication android login google-plus android-pendingintent
我想让用户使用Google+登录.我在Google开发者控制台中创建了该项目,我获得了客户端ID,并在Android Studio中安装了Google Play服务.(我的运行设备有Android 4.4.2)当应用程序运行时我按下登录按钮会出现一个Toast消息:"Internal Error"
Logcat中的错误如下:
error ConnectionResult{statusCode=SIGN_IN_REQUIRED, resolution=PendingIntent{429ffe38: android.os.BinderProxy@429ffdd8}}
Run Code Online (Sandbox Code Playgroud)
请注意,在用户按下按钮之前出现此错误,事实上,通过调试,我发现它来自此行代码:
Log.e(TAG, "error " + result.toString());
Run Code Online (Sandbox Code Playgroud)
在方法中:
public void onConnectionFailed(ConnectionResult result) {
if (!mGoogleIntentInProgress) {
/* Store the ConnectionResult so that we can use it later when the user clicks on the Google+ login button */
mGoogleConnectionResult = result;
if (mGoogleLoginClicked) {
/* The user has already clicked login so we attempt to resolve all errors until the user is signed in,
* or they cancel. */
resolveSignInError();
} else {
Log.e(TAG, "error " + result.toString());
}
}
}
Run Code Online (Sandbox Code Playgroud)
这里有其他重要的方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
SignInButton googleLoginButton = (SignInButton) findViewById(R.id.login_with_google);
googleLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mGoogleLoginClicked = true;
if (!mGoogleApiClient.isConnecting()) {
if (mGoogleConnectionResult != null) {
resolveSignInError();
} else if (mGoogleApiClient.isConnected()) {
getGoogleOAuthTokenAndLogin();
} else {
/* connect API now */
Log.d(TAG, "Trying to connect to Google API");
mGoogleApiClient.connect();
}
}
}
});
/* Setup the Google API object to allow Google+ logins */
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();
}
private void resolveSignInError() {
if (mGoogleConnectionResult.hasResolution()) {
try {
mGoogleIntentInProgress = true;
mGoogleConnectionResult.startResolutionForResult(this, RC_GOOGLE_LOGIN);
} catch (IntentSender.SendIntentException e) {
// The intent was canceled before it was sent. Return to the default
// state and attempt to connect to get an updated ConnectionResult.
mGoogleIntentInProgress = false;
mGoogleApiClient.connect();
}
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_GOOGLE_LOGIN) {
/* This was a request by the Google API */
if (resultCode != RESULT_OK) {
mGoogleLoginClicked = false;
}
mGoogleIntentInProgress = false;
if (!mGoogleApiClient.isConnecting()) {
mGoogleApiClient.connect();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这里是AndroidManifest.xml中的代码
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.INTERNET" />
Run Code Online (Sandbox Code Playgroud)
和元数据
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
Run Code Online (Sandbox Code Playgroud)
希望你能帮助我,非常感谢!
与我的项目一样,Google+登录时存在许多问题.例如,创建时mGoogleApiClient,您应该这样做
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Plus.API)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.addScope(Plus.SCOPE_PLUS_PROFILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
Run Code Online (Sandbox Code Playgroud)
使用SCOPE_PLUS_PROFILE - OAuth 2.0范围访问用户的Google+个人资料数据.
您需要启用Google+ API并使用SHA1和您的包创建凭据.
OAuth同意屏幕,包含您的电子邮件和产品名称 (必填)
更多的,请确认:
的applicationID在标签<应用程序>
的包名在AndroidManifest.xml
包名在谷歌开发者控制台凭证
所有相同
更多,请确保您拥有权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
Run Code Online (Sandbox Code Playgroud)
更多,方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data {
...
...
}
不要需要调用super.onActivityResult(requestCode,resultCode为,数据);
更重要的是:调试密钥库和发布密钥库.
和更多 .....
| 归档时间: |
|
| 查看次数: |
5821 次 |
| 最近记录: |