1

我尝试处理想要从他们的谷歌分析帐户获取一些数据的用户的访问密钥。该代码完美运行了 1 天,但现在我无法再访问谷歌帐户了。

在我看来,Activity 无法再获得令牌。也许它应该更新,肯定不行。似乎是关于谷歌播放的东西!如果我看logcat。我上传了 Oncreate 方法和 logcat,希望有人可以通过查看 logcat 找到问题,在代码没有从 Token 请求中取回正确的变量后,我切断了 logcat。

请帮助我,有什么问题?

@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);

日志猫:

    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)
4

1 回答 1

1

我终于知道了如何使用分析范围登录到谷歌帐户。这是最终的代码。

在谷歌云控制台制作项目时,请确保您注册的应用程序名称与您的Android项目名称相同,否则您将无法获得正确的权限。

完整代码在这里:

    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();
          }
        });
      }
    }
于 2013-10-13T15:01:10.247 回答