After looking thru lots of pages (mostly contradict) I'm not able to write a simple Android app that does connect to Google with OAuth2:
I started with this page:
https://developers.google.com/google-apps/tasks/oauth-and-tasks-on-android#account
I did download the current libs and had to experience lots of deprecated or missing functions:
What is the most current description? What's the most current demo?
Just in case - here's what I did so far. None of the methods in processTokenReceived are part of the libs:
public class ActivityMain extends Activity implements DialogInterface.OnClickListener {
private static final String AUTH_TOKEN_TYPE = "oauth2:https://www.googleapis.com/auth/drive";
private static final String CLIENT_ID = "xxxxxxxxxxxx.apps.googleusercontent.com";
private AccountManager accountManager;
private Account[] accounts;
private String authName;
private String authToken;
@Override
public void onClick(final DialogInterface dialogInterface, final int item) {
processAccountSelected(accounts[item]);
}
@Override
public void onCreate(final Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activitymain);
accountManager = AccountManager.get(this);
accounts = accountManager.getAccountsByType("com.google");
if (accounts == null || accounts.length == 0) {
// TODO
} else if (accounts.length == 1) {
processAccountSelected(accounts[0]);
} else if (accounts.length > 1) {
showDialog(MyConstants.DIALOG_ACCOUNTCHOSER);
}
}
@Override
protected Dialog onCreateDialog(final int id) {
switch (id) {
case MyConstants.DIALOG_ACCOUNTCHOSER:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
String[] names = new String[accounts.length];
for (int i = 0; i < accounts.length; i++) {
names[i] = accounts[i].name;
}
alertDialogBuilder.setItems(names, this);
alertDialogBuilder.setTitle("Select a Google account");
return alertDialogBuilder.create();
}
return null;
}
private void processAccountSelected(final Account account) {
if (account != null) {
authName = account.name.toString();
if (!Tools.isEmpty(authName)) {
Toast.makeText(this, authName, Toast.LENGTH_LONG).show();
accountManager.getAuthToken(account, AUTH_TOKEN_TYPE, null, this,
new AccountManagerCallback<Bundle>() {
public void run(final AccountManagerFuture<Bundle> future) {
try {
authToken = future.getResult().getString(
AccountManager.KEY_AUTHTOKEN);
processTokenReceived();
} catch (OperationCanceledException exception) {
// TODO
} catch (Exception exception) {
Log.d(this.getClass().getName(), exception.getMessage());
}
}
}, null);
}
}
}
private void processTokenReceived() {
if (!Tools.isEmpty(authToken)) {
HttpTransport transport = AndroidHttp.newCompatibleTransport();
AccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(
accessToken);
Tasks service = new Tasks(transport, accessProtectedResource, new JacksonFactory());
service.accessKey = CLIENT_ID;
service.setApplicationName("Google-DriveSample/1.0");
}
}
}
The most recent example for using AccountManager
in combination with a google-api-services library is the updated Tasks API example . GoogleAccountManager
was eg just moved to another package .
If you are not using a build manager like Maven that resolves missing dependencies for you, you of course have to include the google-api-java-client libraries manually, which provide HttpTransport
, etc.
For using AccountManager
it is sufficient to use the Simple API Access key, that is provided in the API console. The part with a certificate fingerprint will only be relevant once Google Play Services will be available.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.