Java Code Examples for com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential#usingOAuth2()

The following examples show how to use com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential#usingOAuth2() . 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: AndroidGoogleDrive.java    From QtAndroidTools with MIT License 9 votes vote down vote up
public boolean authenticate(String AppName, String ScopeName)
{
    final GoogleSignInAccount SignInAccount = GoogleSignIn.getLastSignedInAccount(mActivityInstance);

    if(SignInAccount != null)
    {
        GoogleAccountCredential AccountCredential;
        Drive.Builder DriveBuilder;

        AccountCredential = GoogleAccountCredential.usingOAuth2(mActivityInstance, Collections.singleton(ScopeName));
        AccountCredential.setSelectedAccount(SignInAccount.getAccount());

        DriveBuilder = new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), AccountCredential);
        DriveBuilder.setApplicationName(AppName);
        mDriveService = DriveBuilder.build();

        return true;
    }

    Log.d(TAG, "You have to signin by select account before use this call!");
    return false;
}
 
Example 2
Source File: BaseMainDbActivity.java    From dbsync with Apache License 2.0 6 votes vote down vote up
private void handleSignInONRecnnect() {
    GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);

    if  (account != null ){
        Log.d(TAG, "Signed in as " + account.getEmail());

        // Use the authenticated account to sign in to the Drive service.
        GoogleAccountCredential credential =
                GoogleAccountCredential.usingOAuth2(
                        this, Collections.singleton(DriveScopes.DRIVE_FILE));
        credential.setSelectedAccount(account.getAccount());

        googleDriveService =
                new com.google.api.services.drive.Drive.Builder(
                        AndroidHttp.newCompatibleTransport(),
                        new GsonFactory(),
                        credential)
                        .setApplicationName("Drive API DBSync")
                        .build();
    }
}
 
Example 3
Source File: TGDriveBrowserLogin.java    From tuxguitar with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void process() {
	this.authRequestResultHandler = this.createAuthRequestResultHandler();
	this.accountRequestResultHandler = this.createAccountRequestResultHandler();
	
	this.activity.getResultManager().addHandler(this.authRequestCode, this.authRequestResultHandler);
   	this.activity.getResultManager().addHandler(this.accountRequestCode, this.accountRequestResultHandler);
   	
	this.credential = GoogleAccountCredential.usingOAuth2(this.activity, Collections.singleton(DriveScopes.DRIVE));
	if(!this.settings.isDefaultAccount()) {
		this.credential.setSelectedAccountName(this.settings.getAccount());
	} else {
		String defaultAccount = this.getDefaultAccount();
		if( defaultAccount != null ) {
			this.credential.setSelectedAccountName(defaultAccount);
		}
	}
	this.createTokenAsyncTask().execute((Void) null);
}
 
Example 4
Source File: Auth.java    From UTubeTV with The Unlicense 6 votes vote down vote up
public static GoogleAccountCredential getCredentials(Context ctx, boolean useDefaultAccount) {
  if (credential == null) {
    List<String> scopes = Arrays.asList(YouTubeScopes.YOUTUBE);

    credential = GoogleAccountCredential.usingOAuth2(ctx.getApplicationContext(), scopes);

    // add account name if we have it
    String accountName = null;

    if (useDefaultAccount)
      accountName = accountName(ctx);

    if (accountName != null)
      credential.setSelectedAccountName(accountName);
  }

  return credential;
}
 
Example 5
Source File: YouTubeSearch.java    From YouTube-In-Background with MIT License 5 votes vote down vote up
public YouTubeSearch(Activity activity, Fragment playlistFragment)
{
    this.activity = activity;
    this.playlistFragment = playlistFragment;
    handler = new Handler();
    credential = GoogleAccountCredential.usingOAuth2(activity.getApplicationContext(),
            Arrays.asList(Auth.SCOPES));

    // set exponential backoff policy
    credential.setBackOff(new ExponentialBackOff());
    appName = activity.getResources().getString(R.string.app_name);
    language = Locale.getDefault().getLanguage();
}
 
Example 6
Source File: SendToGoogleUtils.java    From mytracks with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the google account credential.
 * 
 * @param context the context
 * @param accountName the account name
 * @param scope the scope
 */
public static GoogleAccountCredential getGoogleAccountCredential(
    Context context, String accountName, String scope) throws IOException, GoogleAuthException {
  GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context, scope);
  credential.setSelectedAccountName(accountName);
  credential.getToken();
  return credential;
}
 
Example 7
Source File: RestApiActivity.java    From google-services with Apache License 2.0 5 votes vote down vote up
@Override
protected List<Person> doInBackground(Account... accounts) {
    if (mActivityRef.get() == null) {
        return null;
    }

    Context context = mActivityRef.get().getApplicationContext();
    try {
        GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(
                context,
                Collections.singleton(CONTACTS_SCOPE));
        credential.setSelectedAccount(accounts[0]);

        PeopleService service = new PeopleService.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                .setApplicationName("Google Sign In Quickstart")
                .build();

        ListConnectionsResponse connectionsResponse = service
                .people()
                .connections()
                .list("people/me")
                .setFields("names,emailAddresses")
                .execute();

        return connectionsResponse.getConnections();

    } catch (UserRecoverableAuthIOException recoverableException) {
        if (mActivityRef.get() != null) {
            mActivityRef.get().onRecoverableAuthException(recoverableException);
        }
    } catch (IOException e) {
        Log.w(TAG, "getContacts:exception", e);
    }

    return null;
}
 
Example 8
Source File: TaskListFragment.java    From SimplePomodoro-android with MIT License 5 votes vote down vote up
private void initGoogleTask(){
		Logger.getLogger("com.google.api.client").setLevel(LOGGING_LEVEL);
		credential =
		        GoogleAccountCredential.usingOAuth2(getActivity(), Collections.singleton(TasksScopes.TASKS));
		credential.setSelectedAccountName(SettingUtility.getAccountName());
		// Tasks client
		service =
		        new com.google.api.services.tasks.Tasks.Builder(transport, jsonFactory, credential)
		            .setApplicationName("SimplePomodoro").build();
		checkGooglePlayServicesAvailable();
//		}
	}
 
Example 9
Source File: SendToGoogleUtils.java    From mytracks with Apache License 2.0 3 votes vote down vote up
/**
 * Gets the OAuth2 token.
 * 
 * @param context the context
 * @param accountName the account name
 * @param scope the scope
 */
public static String getToken(Context context, String accountName, String scope)
    throws IOException, GoogleAuthException {
  GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context, scope);
  credential.setSelectedAccountName(accountName);
  return credential.getToken();
}