Skip to content

Instantly share code, notes, and snippets.

@bunjix
Created October 7, 2014 10:12
Show Gist options
  • Save bunjix/178a633c3e655b5e9863 to your computer and use it in GitHub Desktop.
Save bunjix/178a633c3e655b5e9863 to your computer and use it in GitHub Desktop.
public void connectFit() {
if (TextUtils.isEmpty(mGoogleMail)) {
Intent intent = AccountPicker.newChooseAccountIntent(null, null, new String[]{"com.google"},
false, null, null, null, null);
startActivityForResult(intent, GOOGLE_ACCOUNT_PICKER_REQUEST);
return;
}
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
try {
return GoogleAuthUtil.getToken(Help.appCtx(), mGoogleMail,
"oauth2:https://www.googleapis.com/auth/fitness");
} catch (IOException transientEx) {
// network or server error, the call is expected to succeed if you try again later.
// Don't attempt to call again immediately - the request is likely to
// fail, you'll hit quotas or back-off.
transientEx.printStackTrace();
return null;
} catch (UserRecoverableAuthException e) {
// Requesting an authorization code will always throw
// UserRecoverableAuthException on the first call to GoogleAuthUtil.getToken
// because the user must consent to offline access to their data. After
// consent is granted control is returned to your activity in onActivityResult
// and the second call to GoogleAuthUtil.getToken will succeed.
startActivityForResult(e.getIntent(), GOOGLE_TOKEN_REQUEST);
return null;
} catch (GoogleAuthException authEx) {
// Failure. The call is not expected to ever succeed so it should not be
// retried.
authEx.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(String token) {
if (TextUtils.isEmpty(token)) {
//TODO Handle Error properly
return;
}
SharedPrefUtils.DEFAULT.save(GOOGLE_FIT_TOKEN, token);
linkToGoogleFit(token);
}
}.execute();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment