5

I am generating token using GoogleUtilAuth.getToken().I have also generated two client ids one for webpage and one for android application and both are in same project .Followed this link

verification from back end server

here is my code :

package com.example.tokengenerate;

import java.io.IOException;

import com.google.android.gms.auth.GoogleAuthException;
import com.google.android.gms.auth.GoogleAuthUtil;
import com.google.android.gms.auth.UserRecoverableAuthException;
import com.google.android.gms.common.Scopes;

import android.os.AsyncTask;
import android.os.Bundle;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
    TextView tv;
    String scope="audience:server:client_id:CLIENTID OF WEBPAGE";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv =(TextView)findViewById(R.id.printId);
    new AsyncTask<Void, Void, Void>() {
        String id=null;
        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            tv.setText(id);
            super.onPostExecute(result);
        }

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub

                String[] account=getAccountNames();
                for(int i =0;i<account.length;i++)
                {
                    try {
                    Log.e("account name", account[i]);
                     id=GoogleAuthUtil.getToken(MainActivity.this, account[i], scope);
                        Log.e("google id",id);
                    } catch (UserRecoverableAuthException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (GoogleAuthException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }



            return null;
        }
    }.execute(null,null,null);


    }
    private String[] getAccountNames() {
      AccountManager  mAccountManager = AccountManager.get(this);
        Account[] accounts = mAccountManager.getAccountsByType(
                GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE);
        String[] names = new String[accounts.length];
        for (int i = 0; i < names.length; i++) {
            names[i] = accounts[i].name;
        }
        return names;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

Here is my Logcat:

08-27 19:19:54.641: W/System.err(9792): com.google.android.gms.auth.GoogleAuthException: Unknown
08-27 19:19:54.651: W/System.err(9792):     at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
08-27 19:19:54.651: W/System.err(9792):     at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
08-27 19:19:54.651: W/System.err(9792):     at com.example.tokengenerate.MainActivity$1.doInBackground(MainActivity.java:45)
08-27 19:19:54.661: W/System.err(9792):     at com.example.tokengenerate.MainActivity$1.doInBackground(MainActivity.java:1)
08-27 19:19:54.661: W/System.err(9792):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
08-27 19:19:54.661: W/System.err(9792):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-27 19:19:54.661: W/System.err(9792):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-27 19:19:54.661: W/System.err(9792):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-27 19:19:54.661: W/System.err(9792):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-27 19:19:54.661: W/System.err(9792):     at java.lang.Thread.run(Thread.java:864)

Totally stuck please help.Not getting where I am going wrong

Sneha Bansal
  • 941
  • 1
  • 13
  • 38
  • Could we see the output from Log.e("account name")? On first glance, your code looks reasonable – Tim Bray Aug 28 '13 at 19:42
  • @TimBray Thank you sir for replying.In account name it is showing emial Id eg "xyz@gmail.com" also it is giving this error only when I put scope=audience:server:client_id:CLIENTID OF WEBPAGE.But if I put scope = https://www.googleapis.com/auth/plus then I got the token. – Sneha Bansal Aug 29 '13 at 13:00
  • 1
    You might try adding `oauth2:` prefix to the scope. – Victor Sergienko Sep 24 '13 at 12:04
  • @Bansal_Sneha, did u get it working? I am too stuck with oauth. What did u do to get it working? – Gopinath Dec 18 '13 at 11:58
  • @Gopinath I just used sample demo from sdk named auth and it is working fine – Sneha Bansal Dec 18 '13 at 12:44
  • 2
    My app has been running just fine during development but as soon as I try a release APK then I get this same error. I think it has to do with the signing key not matching the SHA1 fingerprint entered in the APIs & Auth / Credentials page of the App-Engine project. However, I don't see any way to enter two fingerprints for the same app/clientid. – Brian White Jan 05 '14 at 02:17
  • Check my answer here: http://stackoverflow.com/a/25766367/599628 – CheatEx Sep 10 '14 at 13:12

1 Answers1

0

Use this code to get the scope

String scope = "oauth2:" + Scopes.PROFILE;

and use this scpe here

id=GoogleAuthUtil.getToken(MainActivity.this, account[i], scope);

Then you can get your access tocken in variable "id".Simple method.

Hari Krishnan
  • 5,992
  • 9
  • 37
  • 55