Java Code Examples for com.google.android.gms.auth.GoogleAuthUtil#getToken()

The following examples show how to use com.google.android.gms.auth.GoogleAuthUtil#getToken() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MainActivity.java    From snippets-android with Apache License 2.0 6 votes vote down vote up
public void getAuthToken() {
    // [START fcm_get_token]
    String accountName = getAccount();

    // Initialize the scope using the client ID you got from the Console.
    final String scope = "audience:server:client_id:"
            + "1262xxx48712-9qs6n32447mcj9dirtnkyrejt82saa52.apps.googleusercontent.com";

    String idToken = null;
    try {
        idToken = GoogleAuthUtil.getToken(this, accountName, scope);
    } catch (Exception e) {
        Log.w(TAG, "Exception while getting idToken: " + e);
    }
    // [END fcm_get_token]
}
 
Example 2
Source File: MainActivity.java    From SyncManagerAndroid-DemoGoogleTasks with MIT License 6 votes vote down vote up
/**
 * Gets an authentication token from Google and handles any
 * GoogleAuthException that may occur.
 */
protected String fetchToken() throws IOException {
    try {
        return GoogleAuthUtil.getToken(MainActivity.this, mEmailText, mScope);
    } catch (UserRecoverableAuthException userRecoverableException) {
        // GooglePlayServices.apk is either old, disabled, or not present
        // so we need to show the user some UI in the activity to recover.
        //mActivity.handleException(userRecoverableException);
    	//Log.e(TAG, userRecoverableException.getMessage(), userRecoverableException);
    	
    	mException = userRecoverableException;
    } catch (GoogleAuthException fatalException) {
        // Some other type of unrecoverable exception has occurred.
        // Report and log the error as appropriate for your app.
    	Log.e(TAG, fatalException.getMessage(), fatalException);
    }
    return null;
}
 
Example 3
Source File: GoogleAccountCredential.java    From google-api-java-client with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an OAuth 2.0 access token.
 *
 * <p>
 * Must be run from a background thread, not the main UI thread.
 * </p>
 */
public String getToken() throws IOException, GoogleAuthException {
  if (backOff != null) {
    backOff.reset();
  }

  while (true) {
    try {
      return GoogleAuthUtil.getToken(context, accountName, scope);
    } catch (IOException e) {
      // network or server error, so retry using back-off policy
      try {
        if (backOff == null || !BackOffUtils.next(sleeper, backOff)) {
          throw e;
        }
      } catch (InterruptedException e2) {
        // ignore
      }
    }
  }
}
 
Example 4
Source File: DriveSyncService.java    From narrate-android with Apache License 2.0 5 votes vote down vote up
public DriveSyncService() throws UserRecoverableAuthException {
    super(SyncService.GoogleDrive);

    try {
        HttpTransport httpTransport = new NetHttpTransport();
        JsonFactory jsonFactory = new JacksonFactory();

        String token = GoogleAuthUtil.getToken(GlobalApplication.getAppContext(), Settings.getEmail(), "oauth2:" + DriveScopes.DRIVE_APPDATA);

        if (SettingsUtil.shouldRefreshGDriveToken()) {
            GoogleAuthUtil.clearToken(GlobalApplication.getAppContext(), token);
            token = GoogleAuthUtil.getToken(GlobalApplication.getAppContext(), Settings.getEmail(), "oauth2:" + DriveScopes.DRIVE_APPDATA);
            SettingsUtil.refreshGDriveToken();
        }

        if (BuildConfig.DEBUG)
            LogUtil.log(getClass().getSimpleName(), "Access Token: " + token);

        GoogleCredential credential = new GoogleCredential().setAccessToken(token);
        service = new Drive.Builder(httpTransport, jsonFactory, credential).setApplicationName("Narrate").build();

    } catch (UserRecoverableAuthException ue) {
        throw ue;
    } catch (Exception e) {
        LogUtil.log(getClass().getSimpleName(), "Exception in creation: " + e);
        if (!BuildConfig.DEBUG) Crashlytics.logException(e);
    }

    if (CLEAR_FOLDER_CONTENTS) {
        deleteEverything();
    }
}
 
Example 5
Source File: RNGoogleSigninModule.java    From google-signin with MIT License 5 votes vote down vote up
private void insertAccessTokenIntoUserProperties(RNGoogleSigninModule moduleInstance, WritableMap userProperties) throws IOException, GoogleAuthException {
    String mail = userProperties.getMap("user").getString("email");
    String token = GoogleAuthUtil.getToken(moduleInstance.getReactApplicationContext(),
            new Account(mail, "com.google"),
            scopesToString(userProperties.getArray("scopes")));

    userProperties.putString("accessToken", token);
}
 
Example 6
Source File: GoogleAuthHelper.java    From ribot-app-android with Apache License 2.0 5 votes vote down vote up
public String retrieveAuthToken(Account account)
        throws GoogleAuthException, IOException {

    String token = GoogleAuthUtil.getToken(mContext, account, SCOPE);
    // Token needs to be clear so we make sure next time we get a brand new one. Otherwise this
    // may return a token that has already been used by the API and because it's a one time
    // token it won't work.
    GoogleAuthUtil.clearToken(mContext, token);
    return token;
}
 
Example 7
Source File: GoogleOauth2.java    From edx-app-android with Apache License 2.0 5 votes vote down vote up
/**
 * Gets an authentication token from Google and handles any
 * GoogleAuthException that may occur.
 */
protected String fetchToken() throws IOException {
    if ( activity == null )
        return null;
    try {
        logger.debug("Fetching google oauth2 token ...");
        return GoogleAuthUtil.getToken(activity, mEmail, mScope);
    } catch (UserRecoverableAuthException userRecoverableException) {
        // GooglePlayServices.apk is either old, disabled, or not present
        // so we need to show the user some UI in the activity to recover.
        logger.debug("User recoverable error occurred");
        logger.error(userRecoverableException);
        
        // 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.
        if (activity != null && userRecoverableException.getIntent() != null) {
            activity.startActivityForResult(userRecoverableException.getIntent(), REQUEST_AUTHORIZATION);
        }
    } catch (GoogleAuthException fatalException) {
        logger.warn("google auth error occurred");
        // Some other type of unrecoverable exception has occurred.
        // Report and log the error as appropriate for your app.
        logger.error(fatalException);
    }
    return null;
}
 
Example 8
Source File: GoogleAccountsService.java    From narrate-android with Apache License 2.0 2 votes vote down vote up
/**
 * This method is used to retrieve an Oauth2 token to authenticate with Google's REST APIs.
 * For some reason, using Google Play Service's
 *
 * @param scope This is the Authorization scope that defines the permissions for the Oauth token.
 *              For more info on the scopes for Google Drive, see:
 *              https://developers.google.com/drive/v3/web/about-auth
 *
 * @return Oauth2 token that can be used as a Bearer to authenticate with Google's REST APIs
 */
public static String getAuthToken(String scope) throws MissingGoogleAccountException, PermissionsException, IOException, GoogleAuthException {
    Account acc = getAccount();
    String token = GoogleAuthUtil.getToken(GlobalApplication.getAppContext(), acc, "oauth2:" + scope);
    return token;
}