1

经过漫长的几天,我查看了该领域的几乎所有解决方案,我放弃了。我尝试使用谷歌驱动器,但已经在最简单的代码中失败了。编码:

static String accountName = "xxx@gmail.com"; //my personal google account for trial purposes
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);        
    setContentView(R.layout.sync);

    syncbutton = (ImageButton) findViewById(R.id.syncButton);
    syncbutton.setOnClickListener(new OnClickListener() {

    @SuppressWarnings("deprecation")
    @Override
    public void onClick(View v) {
           Context synccontext = getApplicationContext();

           GoogleAccountCredential credential;

             String scope = "oauth2:https://www.googleapis.com/auth/drive.appdata";

           //from some reason the eclipse mark the usingOAuth2 as deprecated, I failed to        figure out why
           credential = GoogleAccountCredential.usingOAuth2(synccontext, scope);

           credential.setSelectedAccountName(accountName);

             try {
              credential.getToken();
              } catch (IOException e2) {
              // TODO Auto-generated catch block
              System.out.println ("token IOException = " + e2);
              } catch (GoogleAuthException e2) {
              // TODO Auto-generated catch block
              System.out.println ("token GoogleAuthException = " + e2);
    }

             service = getDriveService(credential);

在getToken阶段有以下错误信息:

09-01 12:24:48.645: W/dalvikvm(1131): VFY: unable to resolve static field 1914 (auth_client_play_services_err_notification_msg) in Lcom/google/android/gms/R$string;
09-01 12:24:48.645: D/dalvikvm(1131): VFY: replacing opcode 0x60 at 0x001a
09-01 12:24:48.645: I/dalvikvm(1131): DexOpt: unable to optimize static field ref 0x077b at 0x42 in Lcom/google/android/gms/auth/GoogleAuthUtil;.a
09-01 12:24:48.645: I/dalvikvm(1131): DexOpt: unable to optimize static field ref 0x077c at 0x54 in Lcom/google/android/gms/auth/GoogleAuthUtil;.a
09-01 12:24:48.650: I/dalvikvm(1131): DexOpt: unable to optimize static field ref 0x0778 at 0x73 in Lcom/google/android/gms/auth/GoogleAuthUtil;.a
09-01 12:24:48.650: I/dalvikvm(1131): DexOpt: unable to optimize static field ref 0x0779 at 0x76 in Lcom/google/android/gms/auth/GoogleAuthUtil;.a
09-01 12:24:48.650: I/dalvikvm(1131): DexOpt: unable to optimize static field ref 0x0777 at 0x79 in Lcom/google/android/gms/auth/GoogleAuthUtil;.a
09-01 12:24:48.665: E/GoogleAuthUtil(1131): Calling this from your main thread can lead to deadlock and/or ANRs
09-01 12:24:48.665: E/GoogleAuthUtil(1131): java.lang.IllegalStateException: calling this from your main thread can lead to deadlock
09-01 12:24:48.665: E/GoogleAuthUtil(1131):     at com.google.android.gms.auth.GoogleAuthUtil.b(Unknown Source)
09-01 12:24:48.665: E/GoogleAuthUtil(1131):     at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
09-01 12:24:48.665: E/GoogleAuthUtil(1131):     at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
09-01 12:24:48.665: E/GoogleAuthUtil(1131):     at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:277)
09-01 12:24:48.665: E/GoogleAuthUtil(1131):     at com.gal.shoppinglist.SyncActivity$1.onClick(SyncActivity.java:80)
09-01 12:24:48.665: E/GoogleAuthUtil(1131):     at android.view.View.performClick(View.java:4211)
09-01 12:24:48.665: E/GoogleAuthUtil(1131):     at android.view.View$PerformClick.run(View.java:17267)
09-01 12:24:48.665: E/GoogleAuthUtil(1131):     at android.os.Handler.handleCallback(Handler.java:615)
09-01 12:24:48.665: E/GoogleAuthUtil(1131):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-01 12:24:48.665: E/GoogleAuthUtil(1131):     at android.os.Looper.loop(Looper.java:137)
09-01 12:24:48.665: E/GoogleAuthUtil(1131):     at android.app.ActivityThread.main(ActivityThread.java:4898)
09-01 12:24:48.665: E/GoogleAuthUtil(1131):     at java.lang.reflect.Method.invokeNative(Native Method)
09-01 12:24:48.665: E/GoogleAuthUtil(1131):     at java.lang.reflect.Method.invoke(Method.java:511)
09-01 12:24:48.665: E/GoogleAuthUtil(1131):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
09-01 12:24:48.665: E/GoogleAuthUtil(1131):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
09-01 12:24:48.665: E/GoogleAuthUtil(1131):     at dalvik.system.NativeStart.main(Native Method)
09-01 12:24:48.665: D/AndroidRuntime(1131): Shutting down VM
09-01 12:24:48.665: W/dalvikvm(1131): threadid=1: thread exiting with uncaught exception (group=0x4106f2a0)
09-01 12:24:48.670: E/AndroidRuntime(1131): FATAL EXCEPTION: main
09-01 12:24:48.670: E/AndroidRuntime(1131): java.lang.IllegalStateException: calling this from your main thread can lead to deadlock
09-01 12:24:48.670: E/AndroidRuntime(1131):     at com.google.android.gms.auth.GoogleAuthUtil.b(Unknown Source)
09-01 12:24:48.670: E/AndroidRuntime(1131):     at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
09-01 12:24:48.670: E/AndroidRuntime(1131):     at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
09-01 12:24:48.670: E/AndroidRuntime(1131):     at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:277)
09-01 12:24:48.670: E/AndroidRuntime(1131):     at com.gal.shoppinglist.SyncActivity$1.onClick(SyncActivity.java:80)
09-01 12:24:48.670: E/AndroidRuntime(1131):     at android.view.View.performClick(View.java:4211)

我想这是基本的东西,我提前为此道歉。我将非常感谢任何帮助。

4

1 回答 1

0

身份验证失败通常与两件事有关:

  1. API 密钥不匹配,或

  2. 您忘记在Google API 控制台的服务下启用Drive API服务

如果是 2 号,启用服务,然后通过按API 访问下的生成新密钥重新创建密钥

编辑:弃用与您的目标 API 有关。如果 Android 在某个 API 级别之后删除了该命令并且您的 Target API 更高,则表明您将导致问题。您需要为已弃用的命令找到替换命令(如果存在),并根据设备的 API 级别调用特定命令。

于 2013-09-01T09:54:03.233 回答