Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View get_exchange_code.md

arrays.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="scopes">
        <item>https://www.google.com/m8/feeds</item>
        <item>https://www.googleapis.com/auth/userinfo.email</item>
        <item>https://www.googleapis.com/auth/plus.login</item>
        <item>https://www.googleapis.com/auth/userinfo.profile</item>
    </string-array>
</resources>

exchange code method

private String getExchangeCode(){
    String scope = String.format("oauth2:server:client_id:%s:api_scope:%s", getString(R.string.server_client_id),
            TextUtils.join(" ", getResources().getStringArray(R.array.scopes)));
    String exchangeCode = null;
    try {
        exchangeCode = GoogleAuthUtil.getToken(getActivity(), mEmailAddress, scope);
    } catch (GooglePlayServicesAvailabilityException playEx) {
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(
                playEx.getConnectionStatusCode(),
                getActivity(),
                AUTH_REQUEST_CODE);
    } catch (UserRecoverableAuthException recoverableException) {
        Intent recoveryIntent = recoverableException.getIntent();
        startActivityForResult(recoveryIntent, RECOVERABLE_REQUEST_CODE);
    } catch (final GoogleAuthException authEx) {
        // This is likely unrecoverable.
        Log.e("GoogleAuthException", "Unrecoverable authentication exception: " + authEx.getMessage(), authEx);

        getActivity().runOnUiThread(new Runnable() {
            public void run() {
                if(authEx.getMessage().equals("BadUsername")){
                    Toaster.makeToast("The email address '"+mEmailAddress+"' is a bad username");
                } else {
                    Toaster.makeToast(authEx.getMessage());
                }
            }
        });

    } catch (final IOException ioEx) {
        Log.i("IOException", "transient error encountered: " + ioEx.getMessage());
//            doExponentialBackoff();
        getActivity().runOnUiThread(new Runnable() {
            public void run() {
                Toaster.makeToast(ioEx.getMessage());
            }
        });

    } catch (final IllegalArgumentException argEx) {
        Log.i("DEBUG_TAG", "Illegal argument encountered: " + argEx.getMessage());
        getActivity().runOnUiThread(new Runnable() {
            public void run() {
                if(argEx.getMessage().equals("Non existing account " + mEmailAddress)){
                    Toaster.makeToast("The email address '"+mEmailAddress+"' is not registered on this device");
                } else {
                    Toaster.makeToast(argEx.getMessage());
                }
            }
        });
    }
    return exchangeCode;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.