1

我已经从我的端点生成了我的客户端库,并且还为我对端点的经过身份验证的调用传递了适当的凭据(客户端 ID 是 100% 正确的)。

但是当我运行我的代码时,我仍然收到这个错误:

com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAuthIOException
            at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:286)
            at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:859)
            at com.projects.malaysiakini.Home$ContentApi.doInBackground(Home.java:165)
            at com.projects.malaysiakini.Home$ContentApi.doInBackground(Home.java:141)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
     Caused by: com.google.android.gms.auth.GoogleAuthException: Unknown
            at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
            at com.google.android.gms.auth.GoogleAuthUtil.getToken(Unknown Source)
            at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential.getToken(GoogleAccountCredential.java:255)
            at com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential$RequestHandler.intercept(GoogleAccountCredential.java:279)
            at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:859)
            at com.projects.malaysiakini.Home$ContentApi.doInBackground(Home.java:165)
            at com.projects.malaysiakini.Home$ContentApi.doInBackground(Home.java:141)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)

这是我下面的代码:

 private class ContentApi extends AsyncTask<Void,Void,String>{

       @Override
       protected String doInBackground(Void... params) {
           String response = null;


           try {
               settings = getActivity().getSharedPreferences("MApi", Context.MODE_PRIVATE);
               credential = GoogleAccountCredential.usingAudience(getActivity(), CLIENT_ID);
               setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME, null));

               Core.Builder builder = new Core.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), credential);
               builder.setApplicationName(getActivity().getPackageName());

               service = builder.build();



               if (credential.getSelectedAccountName() != null) {
                   // Already signed in, begin app!

                response = service.info().execute().getLang();
               }//end if
               else {
                   chooseAccount();
               }

               }//end try
               catch(IOException ex){
                   Log.d("MkiniCore", ex.getMessage(), ex);
               }//end catch



           return response;
       }//end doInBackground

       @Override
       protected void onPostExecute(String s) {
          textView.setText(s);
       }
   }

我刚刚将代码编辑为下面的答案,但仍然收到此错误

这是帐户选择器的代码:

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case REQUEST_ACCOUNT_PICKER:
                if (data != null && data.getExtras() != null) {
                    String accountName =
                            data.getExtras().getString(
                                    AccountManager.KEY_ACCOUNT_NAME);
                    if (accountName != null) {
                        setSelectedAccountName(accountName);
                        SharedPreferences.Editor editor = settings.edit();
                        editor.putString(PREF_ACCOUNT_NAME, accountName);
                        editor.commit();
                        // User is authorized.
                    }
                }
                break;
        }
    }

这是 setSelectedAccountName 方法

private void setSelectedAccountName(String accountName){
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(PREF_ACCOUNT_NAME, accountName);
        editor.commit();
        credential.setSelectedAccountName(accountName);
        this.accountName = accountName;
    }//end setSelectedAccountName
4

1 回答 1

3

您需要在调用之前在构建器中设置应用程序包名称.build()

builder.setApplicationName(getActivity().getPackageName());

此外,如果这是一个经过身份验证的调用,您将不得不通过帐户选择器运行用户以选择一个 Google 帐户并在凭据中进行设置:

String accountName = ...;  //  Pull from where ever you are storing it
credential.setSelectedAccountName(accountName);

此外,从提供的代码中看不出什么CLIENT_ID是针对观众(范围)的,因此请确保其格式为:

static final String CLIENT_ID = "server:client_id:" + YOUR_WEB_APP_CLIENT_ID;

由于您使用的是云端点生成的库,因此您不必设置根 URL 或使用原始 HTTP 请求/响应。端点生成的库已经在为您进行 HTTP 请求准备和 HTTP 响应 JSON 解析。在没有看到您的云 API 的情况下,我在这里猜测您的“类别”云方法返回了一些建模为Category具有detail可以使用 getter 检索的成员字段的内容getDetail()

builder.setApplicationName(getActivity().getPackageName());
credential.setSelectedAccountName(accountName);
service = builder.build();
Core.Info.Category endpoint = service.info().category("en");
String category = endpoint.execute().getDetail();
于 2015-04-09T17:20:24.433 回答