Why do I get null from retrieving the user's gmail?
Asked Answered
C

2

0

With the following code that retrieves the user's Google account, gmail, I was wondering why I get null from devices like mine (that of course runs on my gmail), whereas it works on my mom's devices:

public class MainActivity extends AppCompatActivity {

    final String TAG = "Sample2";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String test = getEmail(getApplicationContext());
        Log.d(TAG, "Email is: " + test);
        TextView emailTxt = (TextView)findViewById(R.id.emailTxt);
        emailTxt.setText(test);
    }

    private String getEmail(Context context) {
        AccountManager accountManager = AccountManager.get(context);
        Account account = getAccount(accountManager);

        if (account == null) {
            return null;
        } else {
            return account.name;
        }
    }

    private static Account getAccount(AccountManager accountManager) {
        Account[] accounts = accountManager.getAccountsByType("com.google");
        Account account;
        if (accounts.length > 0) {
            account = accounts[0];
        } else {
            account = null;
        }
        return account;
    }
}

I also included the following permission into my Manifest file:

<uses-permission android:name="android.permission.GET_ACCOUNTS" />

Clearly, it's because of my device... But it can't be the only device that's returning null, so I don't know if this is a good approach for a unique string token when verifying payload with in-app billing.

Oh, and here's a screenshot of what I see in my Accounts & Sync settings:

enter image description here

... Is there anything I'm missing here?

Chelyuskin answered 20/4, 2016 at 16:55 Comment(0)
S
0

Based on the document Set the developer payload string. When making purchase requests, you should not use the user's email address in the payload string, since the address may change.

You should pass in a string token that helps your application to identify the user who made the purchase, so that you can later verify that this is a legitimate purchase by that user. For consumable items, you can use a randomly generated string, but for non- consumable items you should use a string that uniquely identifies the user.

Shive answered 21/4, 2016 at 7:1 Comment(2)
Thanks for the response, but that's the thing. For non-consumable items, I can't figure out how to generate a unique string token for each user while the token runs on all of their devices... Hence why I retrieve email addresses and then encrypt them immediately as the token.Chelyuskin
What would you suggest for a token that runs on all of a user's devices?Chelyuskin
N
0

https://developer.android.com/about/versions/oreo/android-8.0-changes.html

Account access and discoverability In Android 8.0 (API level 26), apps can no longer get access to user accounts unless the authenticator owns the accounts or the user grants that access. The GET_ACCOUNTS permission is no longer sufficient. To be granted access to an account, apps should either use AccountManager.newChooseAccountIntent() or an authenticator-specific method. After getting access to accounts, an app can can call AccountManager.getAccounts() to access them.

Android 8.0 deprecates LOGIN_ACCOUNTS_CHANGED_ACTION. Apps should instead use addOnAccountsUpdatedListener() to get updates about accounts during runtime.

For information about new APIs and methods added for account access and discoverability, see Account Access and Discoverability in the New APIs section of this document

Maybe you can try this

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";
private static final int PICK_ACCOUNT_REQUEST = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Intent googlePicker = AccountManager.newChooseAccountIntent(null, null,
            new String[] { "com.google"}, true, null, null, null, null);
    startActivityForResult(googlePicker, PICK_ACCOUNT_REQUEST);
}

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    if (requestCode == PICK_ACCOUNT_REQUEST && resultCode == RESULT_OK) {
        String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
        Log.d(TAG, "Account Name=" + accountName);
        String accountType = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
        Log.d(TAG, "Account type=" + accountType);

        AccountManager accountManager = AccountManager.get(this);
        Account[] accounts = accountManager.getAccounts();
        for (Account a :
                accounts) {
            Log.d(TAG, "type--- " + a.type + " ---- name---- " + a.name);
        }
    }
}

}

Nedneda answered 25/3, 2018 at 9:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.