我已经从我的端点生成了我的客户端库,并且还为我对端点的经过身份验证的调用传递了适当的凭据(客户端 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