我正在尝试将google登录添加到我的Android应用中。我从google开发者网站下载了google-services.json文件。我仍然不确定这个文件是否正确。 我使用的代码来自谷歌开发者网站本身。
我的日志猫状态是:
handleSignInResult:false77445893
" 77445893"是哈希码。
我也为GET_ACCOUNTS添加了运行时权限。
这是我的google-services.json文件:
{
"project_info": {
"project_number": "746238481207",
"project_id": "civil-sprite-154007"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:746238481207:android:83984ccf8a4ecc15",
"android_client_info": {
"package_name": "com.example.george.project"
}
},
"oauth_client": [
{
"client_id": "746238481207-0q17pl680i93e9qgeco3rjp74knipmco.apps.googleusercontent.com",
"client_type": 1,
"android_info": {
"package_name": "com.example.george.project",
"certificate_hash": "887E55694DCD8DA81377DB2F37C6276E64199263"
}
},
{
"client_id": "746238481207-jlitalqjls0f0nnfbk516bma7j76p0a2.apps.googleusercontent.com",
"client_type": 3
}
],
"api_key": [
{
"current_key": "AIzaSyAytEslbivEZYWoulznIIIGQr7UfyVkeX4"
}
],
"services": {
"analytics_service": {
"status": 1
},
"appinvite_service": {
"status": 1,
"other_platform_oauth_client": []
},
"ads_service": {
"status": 1
}
}
}
],
"configuration_version": "1"
}
这是我的java代码:
package com.example.george.project;
/ ** *由George于2016年12月29日创建。 * /
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.OptionalPendingResult;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import java.io.InputStream;
public class GPlusFragment extends Fragment implements GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = "GPlusFragent";
private int RC_SIGN_IN = 9001;
private GoogleApiClient mGoogleApiClient;
private SignInButton signInButton;
private Button signOutButton;
private Button disconnectButton;
private LinearLayout signOutView;
private TextView mStatusTextView;
private ProgressDialog mProgressDialog;
private ImageView imgProfilePic;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Configure sign-in to request the user's ID, email address, and basic
// profile. ID and basic profile are included in DEFAULT_SIGN_IN.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
// Build a GoogleApiClient with access to the Google Sign-In API and the
// options specified by gso.
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.enableAutoManage(getActivity() /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API,gso)
.build();
}
@Override
public void onStart() {
super.onStart();
OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if (opr.isDone()) {
// If the user's cached credentials are valid, the OptionalPendingResult will be "done"
// and the GoogleSignInResult will be available instantly.
Log.d(TAG, "Got cached sign-in");
GoogleSignInResult result = opr.get();
handleSignInResult(result);
} else {
// If the user has not previously signed in on this device or the sign-in has expired,
// this asynchronous branch will attempt to sign in the user silently. Cross-device
// single sign-on will occur in this branch.
showProgressDialog();
opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(GoogleSignInResult googleSignInResult) {
hideProgressDialog();
handleSignInResult(googleSignInResult);
}
});
}
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_gplus, parent, false);
signInButton = (SignInButton) v.findViewById(R.id.sign_in_button);
signOutButton = (Button) v.findViewById(R.id.sign_out_button);
imgProfilePic = (ImageView) v.findViewById(R.id.img_profile_pic);
mStatusTextView = (TextView) v.findViewById(R.id.status);
Bitmap icon = BitmapFactory.decodeResource(getContext().getResources(),R.drawable.user_default);
imgProfilePic.setImageBitmap(ImageHelper.getRoundedCornerBitmap(getContext(),icon, 200, 200, 200, false, false, false, false));
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
});
signOutButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
updateUI(false);
}
});
}
});
return v;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
/*if (result.isSuccess()) {
GoogleSignInAccount acct = result.getSignInAccount();
// Get account information
String mFullName = acct.getDisplayName();
String mEmail = acct.getEmail();
Toast.makeText(getContext(),mFullName,Toast.LENGTH_LONG).show();
}*/
handleSignInResult(result);
}
}
private void handleSignInResult(GoogleSignInResult result) {
Log.d(TAG, "handleSignInResult:" + result.isSuccess() + result.hashCode());
if (result.isSuccess()) {
// Signed in successfully, show authenticated UI.
GoogleSignInAccount acct = result.getSignInAccount();
mStatusTextView.setText(getString(R.string.signed_in_fmt, acct.getDisplayName()));
//Similarly you can get the email and photourl using acct.getEmail() and acct.getPhotoUrl()
if(acct.getPhotoUrl() != null)
new LoadProfileImage(imgProfilePic).execute(acct.getPhotoUrl().toString());
updateUI(true);
} else {
// Signed out, show unauthenticated UI.
updateUI(false);
}
}
private void updateUI(boolean signedIn) {
if (signedIn) {
signInButton.setVisibility(View.GONE);
signOutButton.setVisibility(View.VISIBLE);
} else {
mStatusTextView.setText(R.string.signed_out);
Bitmap icon = BitmapFactory.decodeResource(getContext().getResources(),R.drawable.user_default);
imgProfilePic.setImageBitmap(ImageHelper.getRoundedCornerBitmap(getContext(),icon, 200, 200, 200, false, false, false, false));
signInButton.setVisibility(View.VISIBLE);
signOutButton.setVisibility(View.GONE);
}
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
// An unresolvable error has occurred and Google APIs (including Sign-In) will not
// be available.
Log.d(TAG, "onConnectionFailed:" + connectionResult);
}
private void showProgressDialog() {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setMessage(getString(R.string.loading));
mProgressDialog.setIndeterminate(true);
}
mProgressDialog.show();
}
private void hideProgressDialog() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.hide();
}
}
/**
* Background Async task to load user profile picture from url
* */
private class LoadProfileImage extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public LoadProfileImage(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... uri) {
String url = uri[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(url).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
if (result != null) {
Bitmap resized = Bitmap.createScaledBitmap(result,200,200, true);
bmImage.setImageBitmap(ImageHelper.getRoundedCornerBitmap(getContext(),resized,250,200,200, false, false, false, false));
}
}
}
}
请帮帮我。
由于