1

Google アナリティクス アカウントからデータを取得したいユーザーへのアクセス キーを処理しようとしています。コードは 1 日間完全に機能しましたが、Google アカウントにアクセスできなくなりました。

アクティビティがトークンを取得できなくなったようです。おそらく、更新する必要があります。何かが機能していないことは間違いありません。Google Play に関する問題のようです。logcatを見ると。Oncreate メソッドと logcat をアップロードしました。誰かが logcat を見るだけで問題を見つけられることを願っています。コードがトークン要求から正しい変数を取得できなかった後、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

ついに、アナリティクス スコープを使用して、Google アカウントにログインする方法を正確に見つけました。これが最終的なコードです。

Google クラウド コンソールでプロジェクトを作成するときは、登録したアプリの名前が 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 に答える