7

I have problem with my project, where I want to connect Google Cloud Endpoints with Android. I'm still getting the same error:

05-12 21:50:23.995: W/System.err(17739): com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAuthIOException
05-12 21:50:23.995: W/System.err(17739):    at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:224)
05-12 21:50:24.000: W/System.err(17739):    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:888)
05-12 21:50:24.000: W/System.err(17739):    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:407)
05-12 21:50:24.000: W/System.err(17739):    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:340)
05-12 21:50:24.000: W/System.err(17739):    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:458)
05-12 21:50:24.000: W/System.err(17739):    at com.example.endpointstutorial_android.MainActivity$QueryScoresTask.doInBackground(MainActivity.java:74)
05-12 21:50:24.005: W/System.err(17739):    at com.example.endpointstutorial_android.MainActivity$QueryScoresTask.doInBackground(MainActivity.java:1)
05-12 21:50:24.005: W/System.err(17739):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-12 21:50:24.005: W/System.err(17739):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-12 21:50:24.005: W/System.err(17739):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-12 21:50:24.005: W/System.err(17739):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-12 21:50:24.005: W/System.err(17739):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-12 21:50:24.005: W/System.err(17739):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-12 21:50:24.005: W/System.err(17739):    at java.lang.Thread.run(Thread.java:856)
05-12 21:50:24.005: W/System.err(17739): Caused by: com.google.android.gms.auth.GoogleAuthException: Unknown
05-12 21:50:24.005: W/System.err(17739):    at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
05-12 21:50:24.005: W/System.err(17739):    at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:192)
05-12 21:50:24.010: W/System.err(17739):    at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:217)
05-12 21:50:24.010: W/System.err(17739):    ... 13 more`

My Android code:

public class MainActivity extends Activity {
    private static final String PREF_ACCOUNT_NAME = "pref.account.name";
    public static final String AUDIENCE = "server:client_id:660102392535.apps.googleusercontent.com";
    SharedPreferences settings;
    GoogleAccountCredential credential;
    String accountName;
    static final int REQUEST_ACCOUNT_PICKER = 2;
    Myendpoint service;

    void chooseAccount() {
        startActivityForResult(credential.newChooseAccountIntent(),
                REQUEST_ACCOUNT_PICKER);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        credential = GoogleAccountCredential
                .usingAudience(
                        this,AUDIENCE);

        settings = getSharedPreferences("SafeAndroid", 0);
        setAccountName(settings.getString(PREF_ACCOUNT_NAME, null));

        Myendpoint.Builder builder = new Myendpoint.Builder(
                AndroidHttp.newCompatibleTransport(), new GsonFactory(),
                credential);
        service = builder.build();

        if (credential.getSelectedAccountName() != null) {
            Toast.makeText(getApplicationContext(),
                    "logged as: " + credential.getSelectedAccountName(), 5000)
                    .show();
            sendRequest();
        } else {
            chooseAccount();
        }

    }

    private class QueryScoresTask extends AsyncTask<Void, Void, Void> {
        Context context;

        public QueryScoresTask(Context context) {
            this.context = context;
        }

        protected Void doInBackground(Void... unused) {
            Log.v("AAAAA", "2");
            try {
                MyEntity e = service.myentity().get().execute();
                Log.v("AAAAA", e.toString() + "");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }
    }

    private void setAccountName(String accountName) {
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(PREF_ACCOUNT_NAME, accountName);
        editor.commit();
        credential.setSelectedAccountName(accountName);
        this.accountName = accountName;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case REQUEST_ACCOUNT_PICKER:
            if (data != null && data.getExtras() != null) {
                String accountName = data.getExtras().getString(
                        AccountManager.KEY_ACCOUNT_NAME);
                if (accountName != null) {
                    setAccountName(accountName);
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString(PREF_ACCOUNT_NAME, accountName);
                    editor.commit();
                    Toast.makeText(
                            getApplicationContext(),
                            "logged as: " + credential.getSelectedAccountName(),
                            5000).show();
                    sendRequest();
                }
            }
            break;
        }
    }

    private void sendRequest() {
        QueryScoresTask a = new QueryScoresTask(this);
        a.execute();
    }

}

And my endpoint code:

@Api(name = "myendpoint", version = "v1",description = "This retrieves an instance of MyEntity.", clientIds = {
            "660102392535.apps.googleusercontent.com", "660102392535-dobseln3gde8e6lqnd2v80l3oh6mm1nh.apps.googleusercontent.com" }, audiences = { "660102392535.apps.googleusercontent.com" })
public class MyEndpoint {

    @ApiMethod(httpMethod = "GET", name = "myentity.get", path = "myentity/get")
    public MyEntity getEntity(User user) {
        MyBusinessClass myBusinessClass = new MyBusinessClass();
        MyEntity myEntity = myBusinessClass.getMyEntity();

        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        Entity device = new Entity("gcmid", "keyname");
        device.setProperty("account", user.getEmail());
        device.setProperty("device", "idtelefonu1");
        datastore.put(device);

        return myEntity;
    }
}

I read that some people had that problem, but did not write how to solve it. I created new debug.key and get new sha1 but not work. Also tried to create new Android project with new package - not worked.

jenzz
  • 7,239
  • 6
  • 49
  • 69
user1262348
  • 151
  • 1
  • 4
  • hellow @user1262348 i have same problem as you find Error with Endpoints in Android : GoogleAuthIOException if you find any solution then please help me. – Nirav Mehta Aug 02 '14 at 05:23

4 Answers4

4

Are you using ADT on windows? If yes, then sha1 fingerprint for debug keystore can be obtained in Windows->Preferences->Android->Build for the debug mode.

0

I ran into the same error with the Tic Tac Toe example.

For me, it turned out that I read the instructions wrongly.

In your Android code, try replacing your Android Client key:

"660102392535.apps.googleusercontent.com"

with your Web Client key:

"660102392535-dobseln3gde8e6lqnd2v80l3oh6mm1nh.apps.googleusercontent.com".

I know that doesn't make sense but it worked for me.

Steve

samneric
  • 3,038
  • 2
  • 28
  • 31
  • OK, next step.... When you create your client key in the API Console, make sure that the package name matches the value from AndroidManifest.xml. E.G. package="com.google.devrel.samples.ttt". – samneric May 16 '13 at 14:35
  • Then with your web client key, make sure the URL matches what the Android app is trying to connect to E.G. "https://.appspot.com" – samneric May 16 '13 at 14:37
0

In "AppConstants.java" (name from tutorials) I changed the WEB_CLIENT_ID to the same Web Client ID as in GAE credentials-screen. According to the tutorial, this should be the Android client ID, hence the problems.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
isqb
  • 1
0

My problem was that i created the credentials with the SHA1 from my release keystore. I think you realised that you have to use the debug keystore but maybe you can try to take the release version, export the signed apk, copy it to your phone and install it from there. Worked in my case.

Veit
  • 101
  • 1
  • 5