Reputation: 296
I have generated my client library from my endpoints, and also passed the appropriate credential (the client ID is 100% correct) for my authenticated call to the endpoints.
But I am still getting this error when I run my codes:
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)
This is my code below:
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);
}
}
I just edited the code to answers below but am still getting this errors
This is the code for the Account picker:
@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;
}
}
this is the setSelectedAccountName method
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
Upvotes: 1
Views: 1494
Reputation: 15775
You need to set the application package name in the builder before calling .build()
:
builder.setApplicationName(getActivity().getPackageName());
Also, if this is an authenticated call, you will have had to run the user through the account picker to select a Google account and also set that in the credentials:
String accountName = ...; // Pull from where ever you are storing it
credential.setSelectedAccountName(accountName);
Also, it's not obvious from the provided code what CLIENT_ID
is for the audience (scope), so make sure it is in the format:
static final String CLIENT_ID = "server:client_id:" + YOUR_WEB_APP_CLIENT_ID;
Since you are using cloud endpoint's generated library, you do not have to set the root URL or use the raw HTTP request/response. The endpoint generated library is already doing the HTTP request prep and HTTP response JSON parsing for you. Without seeing your cloud API, I'm guessing here that your cloud method for "category" returns something modeled as Category
which has a member field detail
which can be retrieved with the 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();
Upvotes: 3