access token expire even with refresh token

36 views
Skip to first unread message

Aimane FARISSI

unread,
Mar 8, 2023, 10:30:01 AM3/8/23
to Google Ads API and AdWords API Forum
Hello,

im trying to connect to Google ads api with GoogleAdsClient, after generation of token and refresh token with scopes adwords and business.manage, i can connect and create budget, but after arround 1h the token expire even with a refresh token.

looks like the refresh token of UserCredentials not taken in consideration for GoogleAdsClient.

//call to generate token
    @GetMapping("/auth")
    public String adsAuth() {
        String redirect_uri = defaultHostname + "/ads/save-token";
        ImmutableList<String> scopes = ImmutableList.<String>builder()
                .add("https://www.googleapis.com/auth/adwords", "https://www.googleapis.com/auth/business.manage").build();
        HttpTransport transport = new NetHttpTransport();

        String clientId = googleAdsService.adsApiConfig().getClientId();
        String clientSecret = googleAdsService.adsApiConfig().getClientSecret();
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(transport, jsonFactory, clientId, clientSecret, scopes).build();

        GoogleAuthorizationCodeRequestUrl url = flow.newAuthorizationUrl();
        url.setRedirectUri(redirect_uri);
        url.setApprovalPrompt("force");
        url.setAccessType("offline");
        String authorize_url = url.build();
        return "redirect:"+authorize_url;
    }

//call to build GoogleAdsClient
Properties googleAdsProps = new Properties();
googleAdsProps.put(GoogleAdsClient.Builder.ConfigPropertyKey.LOGIN_CUSTOMER_ID.getPropertyKey(), loginCustomerId);

Credentials userCredentials =
        UserCredentials.newBuilder()
                .setClientId(clientId)
                .setClientSecret(clientSecret)
                .setAccessToken(new AccessToken(token, null))
                .setRefreshToken(refreshToken)
                .build();

GoogleAdsClient adsClient = GoogleAdsClient.newBuilder()
        .setCredentials(userCredentials)
        .fromProperties(googleAdsProps)
        .setDeveloperToken("xxxxxxxxx")
        .build();

Google Ads API Forum Advisor

unread,
Mar 8, 2023, 12:52:36 PM3/8/23
to aim...@virtuocode.com, adwor...@googlegroups.com
Hi,

Thank you for raising this concern to the Google Ads API support team.

Regarding your concern, please see our Generate User Credentials code sample which allows you to generate your user credentials using the supported client libraries. Also, as per this guide, kindly note that OAuth2 access expires after a limited time, an OAuth2 refresh token is used to automatically renew OAuth2 access. I would also recommend checking this guide for generating refresh tokens.

Additionally, you may check this documentation for more information about the refresh token expiration.

Regards,
Google Logo Google Ads API Team


ref:_00D1U1174p._5004Q2jXKnA:ref
Reply all
Reply to author
Forward
0 new messages