在我的android应用程序中,我想集成Google+服务。为此,我参考了快速入门指南链接。根据该链接,我已经完成了所有步骤,但在执行"创建OAuth 2.0客户端ID"的第三步(a、b、c点)时遇到了问题。由于谷歌开发人员已经改变了它的整个控制台设计,我不会从哪里得到OAuth clien ID?其他步骤我都做得很好。我提到了AndroidSDK提供的PlusSampleActivity
,我使用的是google-play-services_lib
库,但该演示无法正常运行。当我尝试登录时,它会打开对话框来选择gmailID。在选择之后,当我更进一步时,它不会登录,并显示toast消息"发生内部错误"。请帮助解决此问题。我引用了许多链接,但其中大多数可能使用旧的播放服务库,因此我对此没有太多想法。我也在stackoverflow上讨论了许多关于这个问题的问题,但它们不符合我的要求。我也试着调试我的代码,但Error log
没有显示任何关于这方面的消息。如果有人知道这个问题的好的教程示例或解决方案,请帮助我解决它。非常感谢。
代码:
package com.google.android.gms.samples.plus;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.plus.PlusClient;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.content.IntentSender;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class SignInActivity extends Activity implements OnClickListener,
PlusClient.ConnectionCallbacks, PlusClient.OnConnectionFailedListener,
PlusClient.OnAccessRevokedListener {
private static final int DIALOG_GET_GOOGLE_PLAY_SERVICES = 1;
private static final int REQUEST_CODE_SIGN_IN = 1;
private static final int REQUEST_CODE_GET_GOOGLE_PLAY_SERVICES = 2;
private TextView mSignInStatus;
private PlusClient mPlusClient;
private SignInButton mSignInButton;
private View mSignOutButton;
private View mRevokeAccessButton;
private ConnectionResult mConnectionResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_in_activity);
mPlusClient = new PlusClient.Builder(this, this, this)
.setActions(MomentUtil.ACTIONS)
.build();
mSignInStatus = (TextView) findViewById(R.id.sign_in_status);
mSignInButton = (SignInButton) findViewById(R.id.sign_in_button);
mSignInButton.setOnClickListener(this);
mSignOutButton = findViewById(R.id.sign_out_button);
mSignOutButton.setOnClickListener(this);
mRevokeAccessButton = findViewById(R.id.revoke_access_button);
mRevokeAccessButton.setOnClickListener(this);
}
@Override
public void onStart() {
super.onStart();
mPlusClient.connect();
}
@Override
public void onStop() {
mPlusClient.disconnect();
super.onStop();
}
@Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.sign_in_button:
int available = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (available != ConnectionResult.SUCCESS) {
showDialog(DIALOG_GET_GOOGLE_PLAY_SERVICES);
return;
}
try {
mSignInStatus.setText(getString(R.string.signing_in_status));
mConnectionResult.startResolutionForResult(this, REQUEST_CODE_SIGN_IN);
} catch (IntentSender.SendIntentException e) {
// Fetch a new result to start.
mPlusClient.connect();
}
break;
case R.id.sign_out_button:
if (mPlusClient.isConnected()) {
mPlusClient.clearDefaultAccount();
mPlusClient.disconnect();
mPlusClient.connect();
}
break;
case R.id.revoke_access_button:
if (mPlusClient.isConnected()) {
mPlusClient.revokeAccessAndDisconnect(this);
updateButtons(false /* isSignedIn */);
}
break;
}
}
@Override
protected Dialog onCreateDialog(int id) {
if (id != DIALOG_GET_GOOGLE_PLAY_SERVICES) {
return super.onCreateDialog(id);
}
int available = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (available == ConnectionResult.SUCCESS) {
return null;
}
if (GooglePlayServicesUtil.isUserRecoverableError(available)) {
return GooglePlayServicesUtil.getErrorDialog(
available, this, REQUEST_CODE_GET_GOOGLE_PLAY_SERVICES);
}
return new AlertDialog.Builder(this)
.setMessage(R.string.plus_generic_error)
.setCancelable(true)
.create();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_SIGN_IN
|| requestCode == REQUEST_CODE_GET_GOOGLE_PLAY_SERVICES) {
if (resultCode == RESULT_OK && !mPlusClient.isConnected()
&& !mPlusClient.isConnecting()) {
// This time, connect should succeed.
mPlusClient.connect();
}
}
}
@Override
public void onAccessRevoked(ConnectionResult status) {
if (status.isSuccess()) {
mSignInStatus.setText(R.string.revoke_access_status);
} else {
mSignInStatus.setText(R.string.revoke_access_error_status);
mPlusClient.disconnect();
}
mPlusClient.connect();
}
@Override
public void onConnected(Bundle connectionHint) {
String currentPersonName = mPlusClient.getCurrentPerson() != null
? mPlusClient.getCurrentPerson().getDisplayName()
: getString(R.string.unknown_person);
mSignInStatus.setText(getString(R.string.signed_in_status, currentPersonName));
updateButtons(true /* isSignedIn */);
}
@Override
public void onDisconnected() {
mSignInStatus.setText(R.string.loading_status);
mPlusClient.connect();
updateButtons(false /* isSignedIn */);
}
@Override
public void onConnectionFailed(ConnectionResult result) {
mConnectionResult = result;
updateButtons(false /* isSignedIn */);
}
private void updateButtons(boolean isSignedIn) {
if (isSignedIn) {
mSignInButton.setVisibility(View.INVISIBLE);
mSignOutButton.setEnabled(true);
mRevokeAccessButton.setEnabled(true);
} else {
if (mConnectionResult == null) {
// Disable the sign-in button until onConnectionFailed is called with result.
mSignInButton.setVisibility(View.INVISIBLE);
mSignInStatus.setText(getString(R.string.loading_status));
} else {
// Enable the sign-in button since a connection result is available.
mSignInButton.setVisibility(View.VISIBLE);
mSignInStatus.setText(getString(R.string.signed_out_status));
}
mSignOutButton.setEnabled(false);
mRevokeAccessButton.setEnabled(false);
}
}
}
我解决了这个问题。由于我无法在新的googleapi控制台中创建OAuth Client ID
,所以我面临着这个问题。然后我在旧的api控制台中从头开始执行所有步骤,我可以生成OAuth ClientID
,现在我可以将它集成到我的应用程序中了。