1

I try to handle access keys to users, who wants to get some data from their google analytics account. The code worked perfectly for 1 day, but now i cant access the google account any more.

It seems to me like the Activity cant get the token anymore. Perhaps it should be renewed, something definitely not working.Seems to be something about google play! if i looking at the logcat. I uploaded the Oncreate method & the logcat, hope someone can find the problem just by looking at the logcat, i cut the logcat after the code doesnt get the right variables back from the Token request.

Please help me, what is wrong ?

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // enable logging
    Logger.getLogger("analyticsproject").setLevel(LOGGING_LEVEL);

    setContentView(R.layout.activity_main);

    credential = GoogleAccountCredential.usingOAuth2(this, AnalyticsScopes.all());

    SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
    credential.setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME, null));

    service = new com.google.api.services.analytics.Analytics.Builder(httpTransport, jsonFactory, credential)
                    .setApplicationName("Analytics Project").build();

    startActivityForResult(credential.newChooseAccountIntent(),REQUEST_ACCOUNT_PICKER);

Log cat:

    10-02 14:41:21.377      108-124/? W/audio_hw_primary﹕ out_write() limiting sleep time 46802 to 39909
    10-02 14:41:21.441    8269-8269/? E/Trace﹕ error opening trace file: No such file or directory (2)
    10-02 14:41:21.445    8269-8275/? E/jdwp﹕ Failed sending reply to debugger: Broken pipe
    10-02 14:41:21.449      108-124/? W/audio_hw_primary﹕ out_write() limiting sleep time 49274 to 39909
    10-02 14:41:21.541    8269-8269/? W/dalvikvm﹕ VFY: unable to resolve static field 4296 (common_google_play_services_install_title) in Lcom/google/android/gms/R$string;
    10-02 14:41:21.585    5540-5540/com.google.android.gms.ui W/EGL_emulation﹕ eglSurfaceAttrib not implemented
    10-02 14:41:21.657    8269-8269/? W/EGL_emulation﹕ eglSurfaceAttrib not implemented
    10-02 14:41:23.289      310-311/? E/SocketStream﹕ readFully was waiting for 764896 bytes, got 49152
    10-02 14:41:23.353    8269-8269/com.example.analyticsproject W/EGL_emulation﹕ eglSurfaceAttrib not implemented
    10-02 14:41:23.357    8269-8284/com.example.analyticsproject W/dalvikvm﹕ VFY: unable to resolve static field 4288 (auth_client_play_services_err_notification_msg) in Lcom/google/android/gms/R$string;
    10-02 14:41:23.861    5477-5496/com.google.android.gsf.login W/GLSActivity﹕ [sj] Status from wire: NeedPermission status: NEED_PERMISSION
    10-02 14:41:23.865    8269-8284/com.example.analyticsproject W/System.err﹕ com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException
    10-02 14:41:23.865    8269-8284/com.example.analyticsproject W/System.err﹕ at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:284)
    10-02 14:41:23.865    8269-8284/com.example.analyticsproject W/System.err﹕ at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:858)
    10-02 14:41:23.865    8269-8284/com.example.analyticsproject W/System.err﹕ at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
    10-02 14:41:23.865    8269-8284/com.example.analyticsproject W/System.err﹕ at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
    10-02 14:41:23.865    8269-8284/com.example.analyticsproject W/System.err﹕ at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
    10-02 14:41:23.865    8269-8284/com.example.analyticsproject W/System.err﹕ at com.example.analyticsproject.Analytics_requests.find_Accounts(Analytics_requests.java:34)
    10-02 14:41:23.865    8269-8284/com.example.analyticsproject W/System.err﹕ at com.example.analyticsproject.MainActivity$Login.doInBackground(MainActivity.java:623)
    10-02 14:41:23.865    8269-8284/com.example.analyticsproject W/System.err﹕ at com.example.analyticsproject.MainActivity$Login.doInBackground(MainActivity.java:602)
    10-02 14:41:23.865    8269-8284/com.example.analyticsproject W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:287)
    10-02 14:41:23.865    8269-8284/com.example.analyticsproject W/System.err﹕ at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    10-02 14:41:23.865    8269-8284/com.example.analyticsproject W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    10-02 14:41:23.865    8269-8284/com.example.analyticsproject W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
    10-02 14:41:23.865    8269-8284/com.example.analyticsproject W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
    10-02 14:41:23.865    8269-8284/com.example.analyticsproject W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
    10-02 14:41:23.865    8269-8284/com.example.analyticsproject W/System.err﹕ at java.lang.Thread.run(Thread.java:856)
    10-02 14:41:23.865    8269-8284/com.example.analyticsproject W/System.err﹕ Caused by: com.google.android.gms.auth.UserRecoverableAuthException: NeedPermission
    10-02 14:41:23.865    8269-8284/com.example.analyticsproject W/System.err﹕ at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
    10-02 14:41:23.865    8269-8284/com.example.analyticsproject W/System.err﹕ at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
    10-02 14:41:23.865    8269-8284/com.example.analyticsproject W/System.err﹕ at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:255)
    10-02 14:41:23.865    8269-8284/com.example.analyticsproject W/System.err﹕ at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:279)
    10-02 14:41:23.865    8269-8284/com.example.analyticsproject W/System.err﹕ ... 14 more
    10-02 14:41:24.337    5477-5499/com.google.android.gsf.login W/GLSActivity﹕ [sj] Status from wire: NeedPermission status: NEED_PERMISSION
    10-02 14:41:24.337    8269-8284/com.example.analyticsproject W/System.err﹕ com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException
    10-02 14:41:24.337    8269-8284/com.example.analyticsproject W/System.err﹕ at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:284)
    10-02 14:41:24.337    8269-8284/com.example.analyticsproject W/System.err﹕ at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:858)
    10-02 14:41:24.337    8269-8284/com.example.analyticsproject W/System.err﹕ at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
    10-02 14:41:24.337    8269-8284/com.example.analyticsproject W/System.err﹕ at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
    10-02 14:41:24.337    8269-8284/com.example.analyticsproject W/System.err﹕ at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
    10-02 14:41:24.337    8269-8284/com.example.analyticsproject W/System.err﹕ at com.example.analyticsproject.Analytics_requests.get_All_Accounts(Analytics_requests.java:66)
    10-02 14:41:24.337    8269-8284/com.example.analyticsproject W/System.err﹕ at com.example.analyticsproject.MainActivity$Login.doInBackground(MainActivity.java:639)
    10-02 14:41:24.337    8269-8284/com.example.analyticsproject W/System.err﹕ at com.example.analyticsproject.MainActivity$Login.doInBackground(MainActivity.java:602)
Bobbake4
  • 24,509
  • 9
  • 59
  • 94
Friis1978
  • 1,179
  • 9
  • 16

1 Answers1

1

Ive finally found out exactly how to log into the google account, using the analytics scope. Here is the final code.

When you making your project in the Google cloud console, make sure that the name of your registered app is the same as your Android project name, if its not, you will not get the right permisions.

The full code is here:

    import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.accounts.AccountManager;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
import com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.analytics.Analytics;
import com.google.api.services.analytics.AnalyticsScopes;
import com.google.api.services.analytics.model.Accounts;
import com.google.api.services.analytics.model.Profiles;
import com.google.api.services.analytics.model.Webproperties;

public class MainActivity extends Activity {
      static final int REQUEST_ACCOUNT_PICKER = 1;
      static final int REQUEST_AUTHORIZATION = 2;    

      private static Analytics analytics_service;
      private GoogleAccountCredential credential;

      private String ProfileId;

      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        credential = GoogleAccountCredential.usingOAuth2(this,
                Arrays.asList(AnalyticsScopes.ANALYTICS,AnalyticsScopes.ANALYTICS_MANAGE_USERS,AnalyticsScopes.ANALYTICS_READONLY));
        startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
      }

      @Override
      protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        switch (requestCode) {
        case REQUEST_ACCOUNT_PICKER:
          if (resultCode == RESULT_OK && data != null && data.getExtras() != null) {
            String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
            if (accountName != null) {
              credential.setSelectedAccountName(accountName);             
              analytics_service = getAnalyticsService(credential);            
              getAnalyticsAccounts();
            }
          }

          break;
        case REQUEST_AUTHORIZATION:
          if (resultCode == Activity.RESULT_OK) {           
            getAnalyticsAccounts();
          } else {
            startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
          }     
        }
      }

      private void getAnalyticsAccounts() {
            Thread t = new Thread(new Runnable() {
              @Override
              public void run() {

                  Accounts accounts;
                  int account_num;
                  Profiles profiles;
                  Webproperties webproperties;                
                  String WebpropertyId;
                  String Id;

                  List<String> list_accounts = new ArrayList<String>();

                  try {

                  accounts = analytics_service.management().accounts().list().execute();              
                  account_num = accounts.getItems().size();

                  Id = accounts.getItems().get(0).getId();
                  Log.d("Analytics_requests", "account_id: " + Id);

                  webproperties = analytics_service.management().webproperties().list(Id).execute();
                  WebpropertyId = webproperties.getItems().get(0).getId();
                  Log.d("Analytics_requests", "property_id: " + WebpropertyId);

                  profiles = analytics_service.management().profiles().list(Id, WebpropertyId).execute();
                  String Profile_Id = profiles.getItems().get(0).getId();
                  Log.d("Analytics_requests", "profile_id: " + Profile_Id);

                  ProfileId = Profile_Id;

                  for (int i = 0; i < account_num; i++) {

                      Log.d("Analytics_requests", "account_name: " + accounts.getItems().get(i).getName());
                      list_accounts.add(accounts.getItems().get(i).getName());
                  }

                  if (list_accounts != null) {
                    showToast("First Account found: " + list_accounts.get(0).toString());
                    showToast("Id found: " + ProfileId);    
                  }
                } catch (UserRecoverableAuthIOException e) {
                  startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
                } catch (IOException e) {
                  e.printStackTrace();
                }
              }
            });
            t.start();
          }

      private Analytics getAnalyticsService(GoogleAccountCredential credential) {
          return new Analytics.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential).build();

      }

      public void showToast(final String toast) {
        runOnUiThread(new Runnable() {
          @Override
          public void run() {
            Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show();
          }
        });
      }
    }
Friis1978
  • 1,179
  • 9
  • 16