Search in sources :

Example 1 with GoogleAuthorizationCodeTokenRequest

use of com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest in project OpenRefine by OpenRefine.

the class GoogleAPIExtension method getTokenFromCode.

public static String getTokenFromCode(ButterflyModule module, HttpServletRequest request) throws IOException {
    String redirectUrl = makeRedirectUrl(module, request);
    StringBuffer fullUrlBuf = request.getRequestURL();
    if (request.getQueryString() != null) {
        fullUrlBuf.append('?').append(request.getQueryString());
    }
    AuthorizationCodeResponseUrl authResponse = new AuthorizationCodeResponseUrl(fullUrlBuf.toString());
    // check for user-denied error
    if (authResponse.getError() != null) {
    // authorization denied...
    } else {
        // request access token using authResponse.getCode()...
        String code = authResponse.getCode();
        GoogleTokenResponse response = new GoogleAuthorizationCodeTokenRequest(HTTP_TRANSPORT, JSON_FACTORY, CLIENT_ID, CLIENT_SECRET, code, redirectUrl).execute();
        String tokenAndExpiresInSeconds = response.getAccessToken() + "," + response.getExpiresInSeconds();
        return tokenAndExpiresInSeconds;
    }
    return null;
}
Also used : GoogleAuthorizationCodeTokenRequest(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest) AuthorizationCodeResponseUrl(com.google.api.client.auth.oauth2.AuthorizationCodeResponseUrl) GoogleTokenResponse(com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse)

Example 2 with GoogleAuthorizationCodeTokenRequest

use of com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest in project OpenRefine by OpenRefine.

the class GDataExtension method getTokenFromCode.

public static String getTokenFromCode(ButterflyModule module, HttpServletRequest request) throws MalformedURLException {
    String redirectUrl = makeRedirectUrl(module, request);
    StringBuffer fullUrlBuf = request.getRequestURL();
    if (request.getQueryString() != null) {
        fullUrlBuf.append('?').append(request.getQueryString());
    }
    AuthorizationCodeResponseUrl authResponse = new AuthorizationCodeResponseUrl(fullUrlBuf.toString());
    // check for user-denied error
    if (authResponse.getError() != null) {
    // authorization denied...
    } else {
        // request access token using authResponse.getCode()...
        String code = authResponse.getCode();
        try {
            GoogleTokenResponse response = new GoogleAuthorizationCodeTokenRequest(HTTP_TRANSPORT, JSON_FACTORY, CLIENT_ID, CLIENT_SECRET, code, redirectUrl).execute();
            String token = response.getAccessToken();
            return token;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return null;
}
Also used : GoogleAuthorizationCodeTokenRequest(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest) AuthorizationCodeResponseUrl(com.google.api.client.auth.oauth2.AuthorizationCodeResponseUrl) GoogleTokenResponse(com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse) IOException(java.io.IOException)

Example 3 with GoogleAuthorizationCodeTokenRequest

use of com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest in project xabber-android by redsolution.

the class BaseLoginActivity method handleSignInResult.

private void handleSignInResult(GoogleSignInResult result) {
    if (result.isSuccess()) {
        GoogleSignInAccount acct = result.getSignInAccount();
        if (acct != null) {
            final String googleAuthCode = acct.getServerAuthCode();
            Application.getInstance().runInBackground(new Runnable() {

                @Override
                public void run() {
                    try {
                        GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(new NetHttpTransport(), JacksonFactory.getDefaultInstance(), GOOGLE_TOKEN_SERVER, getString(R.string.SOCIAL_AUTH_GOOGLE_KEY), getString(R.string.SOCIAL_AUTH_GOOGLE_SECRET), googleAuthCode, "").execute();
                        final String token = tokenResponse.getAccessToken();
                        Application.getInstance().runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                String credentials = gson.toJson(new AuthManager.AccessToken(token));
                                onSocialAuthSuccess(AuthManager.PROVIDER_GOOGLE, credentials);
                            }
                        });
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    } else
        Toast.makeText(this, R.string.auth_google_error, Toast.LENGTH_LONG).show();
}
Also used : GoogleSignInAccount(com.google.android.gms.auth.api.signin.GoogleSignInAccount) AuthManager(com.xabber.android.data.xaccount.AuthManager) GoogleAuthorizationCodeTokenRequest(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) GoogleTokenResponse(com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse) IOException(java.io.IOException)

Example 4 with GoogleAuthorizationCodeTokenRequest

use of com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest in project Android-SDK by Backendless.

the class LoginWithGooglePlusSDKActivity method exchangeAuthTokenOnAccessToken.

private void exchangeAuthTokenOnAccessToken(String gpAuthToken) {
    GoogleTokenResponse tokenResponse;
    try {
        tokenResponse = new GoogleAuthorizationCodeTokenRequest(new NetHttpTransport(), JacksonFactory.getDefaultInstance(), "https://www.googleapis.com/oauth2/v4/token", getString(R.string.gp_WebApp_ClientId), getString(R.string.gp_WebApp_ClientSecret), gpAuthToken, // Specify the same redirect URI that you use with your web
        "").execute();
    } catch (Exception e) {
        Log.e("LoginWithGooglePlus", e.getMessage(), e);
        String msg = socialAccountInfo.getText() + "\n" + e.getMessage();
        socialAccountInfo.setText(msg);
        socialAccountInfo.setTextColor(getColor(android.R.color.holo_red_dark));
        return;
    }
    gpAccessToken = tokenResponse.getAccessToken();
    final String msg = socialAccountInfo.getText() + "\n" + "AccessToken: " + gpAccessToken;
    this.runOnUiThread(() -> socialAccountInfo.setText(msg));
}
Also used : GoogleAuthorizationCodeTokenRequest(com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) GoogleTokenResponse(com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse)

Aggregations

GoogleAuthorizationCodeTokenRequest (com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest)4 GoogleTokenResponse (com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse)4 AuthorizationCodeResponseUrl (com.google.api.client.auth.oauth2.AuthorizationCodeResponseUrl)2 NetHttpTransport (com.google.api.client.http.javanet.NetHttpTransport)2 IOException (java.io.IOException)2 GoogleSignInAccount (com.google.android.gms.auth.api.signin.GoogleSignInAccount)1 AuthManager (com.xabber.android.data.xaccount.AuthManager)1