how do I get a refresh token?

191 views
Skip to first unread message

Rusty Wright

unread,
Jan 4, 2014, 2:47:46 AM1/4/14
to google-oauth...@googlegroups.com
Is it the call to AuthorizationCodeRequestUrl? Or elsewhere?  Here's my code for that:

        final Credential credential = new Credential.
                Builder(BearerToken.authorizationHeaderAccessMethod()).build();

        AppEngineCredentialStore appEngineCredentialStore =
            new AppEngineCredentialStore();

        if (! appEngineCredentialStore.load(stripesActionBeanContext.getUser().getUserId(), credential)) {
            log.debug("calling AuthorizationCodeRequestUrl");

            String url =
                    new AuthorizationCodeRequestUrl("https://accounts.google.com/o/oauth2/auth",
                            Constants.GOOGLE_API_KEY)
                        .setRedirectUri(Constants
                                .googleOJCCallbackUri(stripesActionBeanContext.getRequest().getScheme(),
                                        stripesActionBeanContext.getRequest().getServerName(),
                                        stripesActionBeanContext.getRequest().getServerPort()))
                        .setScopes(Collections.singletonList("https://www.googleapis.com/auth/urlshortener"))
                        .setState(stripesActionBeanContext.getRequest().getRequestURI())
                        //.set("access_type", "offline")
                        .build();

            log.debug("redirect url: {}", url);

            return (new RedirectResolution(url));
        }

My callback has this:

        TokenResponse tokenResponse =
                new AuthorizationCodeTokenRequest(new UrlFetchTransport(),
                    new JacksonFactory(),
                    new GenericUrl("https://accounts.google.com/o/oauth2/token"),
                    this.code)
                        .setRedirectUri(Constants.googleOJCCallbackUri(
                                getContext().getRequest().getScheme(),
                                getContext().getRequest().getServerName(),
                                getContext().getRequest().getServerPort()))
                        .setClientAuthentication(
                                new ClientParametersAuthentication(
                                        Constants.GOOGLE_API_KEY,
                                        Constants.GOOGLE_API_SECRET))
                        .execute();

        Credential credential2 = new Credential.Builder(BearerToken.authorizationHeaderAccessMethod())
                .addRefreshListener(new CredentialStoreRefreshListener(getContext().getUser().getUserId(),
                        new AppEngineCredentialStore()))
                .setJsonFactory(new JacksonFactory())
                .setTokenServerUrl(new GenericUrl("https://accounts.google.com/o/oauth2/token"))
                .setClientAuthentication(
                        new ClientParametersAuthentication(
                                Constants.GOOGLE_API_KEY,
                                Constants.GOOGLE_API_SECRET))
                .setTransport(new UrlFetchTransport())
                .build()
                .setFromTokenResponse(tokenResponse);

        if (! credential2.refreshToken()) {
            this.addMessage("no refresh token");
        }

        AppEngineCredentialStore appEngineCredentialStore = new AppEngineCredentialStore();

        appEngineCredentialStore.store(getContext().getUser().getUserId(), credential2);

This is how I use the Credential.  When the token is expired this code fails.

        HttpRequestFactory requestFactory = new UrlFetchTransport()
            .createRequestFactory(getContext().getCredential());

        HttpResponse httpResponse = requestFactory
            .buildGetRequest(new GenericUrl("https://www.googleapis.com/urlshortener/v1/url/history"))
            .execute();

Thanks!

Rusty Wright

unread,
Jan 6, 2014, 4:40:52 PM1/6/14
to google-oauth...@googlegroups.com
I can get it if I set approval_prompt to force.  Here's the latest version of my code:


Other than having to do that I'm a happy camper.  Thanks for the library google guys.
Reply all
Reply to author
Forward
0 new messages