0

I have been trying to follow the method outlined in the QuickStart guide, but I'm stuck at this.

https://learn.microsoft.com/en-us/advertising/guides/authentication-oauth-get-tokens?view=bingads-13

Bing Ads API Version: 13.0.13

Successfully got the Authorization Code

URL to get the Oauth Code: "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=" + bingClientId.trim() + "&response_type=code&redirect_uri=" + bingRedirectUrl+ "&scope=https://ads.microsoft.com/msads.manage";

As per my knowledge, Using this OAuth Code we need to get the Access token, Refresh token and expires_in in the response. But, I’m getting the following:

  1. Token_type which is the bearer
  2. Scope which is https://ads.microsoft.com/msads.manage
  3. expires_in => 3600
  4. ext_expires_in => 3600

5.access_token

Unfortunately, I’m unable to get the refresh token. Below is my code:

public Map<String, String> getAccessTokens(String bingClientId, String authorizationCode, String bingSecret, String redirectURI, URL url) throws IOException {
         Map<String, String> accessTokens = new HashMap();
         Map<String, String> tokenInfo = new HashMap();
         try {
             LOGGER.info("Microsoft Ads url:" + url);
             String URL = "https://login.microsoftonline.com/common/oauth2/v2.0/token";
             OkHttpClient client = new OkHttpClient();
             Response response;
             MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
             RequestBody body = RequestBody.create(mediaType, "client_id=" + bingClientId.trim() + "&scope=https://ads.microsoft.com/msads.manage&code=" + authorizationCode.trim() + "&redirect_uri=" + redirectURI + "&grant_type=authorization_code&client_secret=" + bingSecret.trim());
             Request request = new Request.Builder()
                     .url(URL)
                     .post(body)
                     .addHeader("Content-Type", "application/x-www-form-urlencoded")
                     .build();
             Call call = client.newCall(request);
             response = call.execute();
             String jsonString = response.body().string();
             MediaType contentType = body.contentType();
             tokenInfo = new ObjectMapper().readValue(jsonString, new TypeReference<Map>() {
             });
             accessTokens.put("accessToken", tokenInfo.get("access_token"));
             return accessTokens;
         } catch (Exception e) {
             LOGGER.info("Exception is due to fetching the Bing tokens.." + e);
         }
         return null;
     }

How can I fix this issue ? Am i sending the different request altogether?

Thanks in advance for your help !

Manju
  • 199
  • 1
  • 2
  • 10

1 Answers1

0

Include offline_access in scope parameter of the consent request.

scope=https://ads.microsoft.com/msads.manage%20offline_access

Nitesh
  • 60
  • 7