Cloud endpoints authentication failure in android app

為{幸葍}努か 提交于 2019-12-30 22:54:43

问题


I'm having trouble with my first attempt to use authentication in debug mode in a Google Cloud Endpoints android app. I set up credentials like this:

credential = GoogleAccountCredential.usingAudience(this,
           "server:client_id:long-string-i-got-from-api-console");
credential.setSelectedAccountName(accountName);

then try to use it like this:

final String LOCAL_APP_ENGINE_SERVER_URL = "http://xxx.xxx.x.xxx:8888"; 
Testdbendpoint.Builder endpointBuilder = new Testdbendpoint.Builder(
            AndroidHttp.newCompatibleTransport(),
            new GsonFactory(),
            credential);
endpointBuilder.setRootUrl(LOCAL_APP_ENGINE_SERVER_URL + "/_ah/api/");
Testdbendpoint endpoint = endpointBuilder.build();
try {
    TestDB testDB = new TestDB().setId(10101L);                      
    TestDB result = endpoint.insertTestDB(testDB).execute();  //-- fails here!!!!
} catch ...

But the try fails and I get these messages in logCat:

03-06 23:33:20.418: W/System.err(11861): Caused by: com.google.android.gms.auth.GoogleAuthException: Unknown 03-06 23:33:20.418: W/System.err(11861): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source) 03-06 23:33:20.423: W/System.err(11861): at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source) 03-06 23:33:20.428: W/System.err(11861): at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:192)


回答1:


Maybe you have the wrong Certificate fingerprint (SHA1) for your Android Client-Id? The authentication with the fingerprint of your production key works only if you sign the .apk manually.

Register a Client-Id for an Installed Application (Android) with your debug.keystore fingerprint in your API Console. To get the fingerprint use:

C:\>keytool -list -alias androiddebugkey -keystore C:\.android\debug.keystore -storepass android -keypass android

Also you need a Web-Client-Id and set it as Audience in your Android application:

credential = GoogleAccountCredential.usingAudience(this,"server:client_id:" + WEB_CLIENT_ID);

AppEngine Endpoint configuration should look like this:

@Api(
    name = "testEndpoint",
    version = "v1",
    clientIds = {ClientIds.WEB_ID, ClientIds.ANDROID_PRODUCTION_ID, ClientIds.ANDROID_DEBUG_ID},
    audiences = {ClientIds.WEB_ID}

)




回答2:


Just making sure, but did you register a client ID in the Google APIs Console as well as an App Engine App ID? And has that google account been added to the device?

The instructions on this blog might be useful:
http://devthots.blogspot.com/

Hope that helps!




回答3:


I wish this might help you.

import android.accounts.Account;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.auth.GoogleAuthException;
import com.google.android.gms.auth.GoogleAuthUtil;
import com.google.android.gms.auth.api.Auth;
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.api.GoogleApiClient;
import com.urbanft.utils.AppToast;

import java.io.IOException;

/**
 * Created by kiwitech on 13/10/16.
 */

public class GoogleLogin extends FragmentActivity implements GoogleApiClient.OnConnectionFailedListener {

    private GoogleSignInOptions gso;
    protected GoogleApiClient mGoogleApiClient;
    private int RC_SIGN_IN = 100;
    public static String GOOGLE_ACCESS_TOKEN = "google_access_token";
    public static String GOOGLE_USER_ID = "google_user_id";
    private String mGooglesUserId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initialize();
    }

    private void initialize(){
        gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .build();

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this , this)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();
    }

    protected void goForGoogleSignIn(){
        Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            if(result.isSuccess()){
                AppToast.showToast(this,"Google sign-in success");
                mGooglesUserId = result.getSignInAccount().getId();
                new LocalAsyncTask(result.getSignInAccount().getEmail()).execute();
            }else{
                AppToast.showToast(this,"Google sign-in failure");
                onBackPressed();
                finish();
            }
        }
    }

    class LocalAsyncTask extends AsyncTask<String,String,String> {

        private String email;

        LocalAsyncTask(String email) {
            this.email = email;
        }

        @Override
        protected String doInBackground(String... params) {
            String token = null;
            try {
                String SCOPE = "oauth2:https://www.googleapis.com/auth/userinfo.profile";
                Account account = new Account(email, "com.google");
                token = GoogleAuthUtil.getToken(GoogleLogin.this, account, SCOPE);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (GoogleAuthException e) {
                e.printStackTrace();
            }
            return token;
        }

        @Override
        protected void onPostExecute(String s){
            Intent intent =new Intent();
            intent.putExtra(GOOGLE_ACCESS_TOKEN,s);
            intent.putExtra(GOOGLE_USER_ID,mGooglesUserId);
            setResult(Activity.RESULT_OK, intent);
            onBackPressed();
            finish();
        }
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    }
}


来源:https://stackoverflow.com/questions/15263228/cloud-endpoints-authentication-failure-in-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!