I'm totally new to using Google API keys with Android, and I found this example because I'm interested in doing a very simple Google Tasks notification app. However, despite having fixed all the Gradle build problems, I can't get the app to work.
When I run it on Android 8.0.0, as soon as I choose a Google account, I get UNREGISTERED_ON_API_CONSOLE
. From searching, I understand it means the APK must be signed and the OAuth 2.0 client ID must be obtained. I have managed to do all that following (more or less) this advice.
The trouble I have is with the (5-year-old) sample code at https://github.com/google/google-api-java-client-samples/tree/master/tasks-android-sample -- I can't see where to specify the Client ID from the Console. The documentation mentions the API key (albeit for quota limitations, not because of security), but not where it is used:
You must also specify the API key from the Google API Console. Otherwise, the token that the AccountManager gives you only provides you with anonymous quota, which is usually very low. By contrast, by specifying an API key you receive a higher free quota, and can optionally set up billing for usage above that.
Example code snippet taken from tasks-android-sample:
com.google.api.services.tasks.Tasks service;
@Override
public void onCreate(Bundle savedInstanceState) {
credential =
GoogleAccountCredential.usingOAuth2(this, Collections.singleton(TasksScopes.TASKS));
SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
credential.setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME, null));
service =
new com.google.api.services.tasks.Tasks.Builder(httpTransport, jsonFactory, credential)
.setApplicationName("Google-TasksAndroidSample/1.0").build();
}
private void chooseAccount() {
startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_GOOGLE_PLAY_SERVICES:
if (resultCode == Activity.RESULT_OK) {
haveGooglePlayServices();
} else {
checkGooglePlayServicesAvailable();
}
break;
case REQUEST_AUTHORIZATION:
if (resultCode == Activity.RESULT_OK) {
AsyncLoadTasks.run(this);
} else {
chooseAccount();
}
break;
case REQUEST_ACCOUNT_PICKER:
if (resultCode == Activity.RESULT_OK && data != null && data.getExtras() != null) {
String accountName = data.getExtras().getString(AccountManager.KEY_ACCOUNT_NAME);
if (accountName != null) {
credential.setSelectedAccountName(accountName);
SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString(PREF_ACCOUNT_NAME, accountName);
editor.commit();
AsyncLoadTasks.run(this);
}
}
break;
}
}
Where do I put the API key information in the approach used here (GoogleAccountCredential.usingOAuth2
) to avoid the UNREGISTERED_ON_API_CONSOLE
error?
Edit: Additional info on this page seems to imply authentication is much more complicated than the sample code.
Ok, I found a one-line fix, but it's far from obvious in the Google documentation I quoted above.
Inside the file AsyncLoadTasks.java
there's a line to fetch the tasks:
List<Task> tasks =
client.tasks().list("@default").setFields("items/title").execute().getItems();
The OAuth 2.0 client ID can be added with the setKey
method.
List<Task> tasks =
client.tasks().list("@default").setFields("items/title")
.setKey("Client_ID_from_API_Console") // Added this call
.execute().getItems();
To clarify what to use from the API Console, it looks like this (after you've set it up):