Unable to get the Refresh Token - Bing Ads API

harshitha reddy y 6 Reputation points
2022-02-11T11:58:40.437+00:00

HI Team,

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

Note: I'm sending this question in different account. My actual developer account is different.

Bing Ads API Version: 13.0.13

  1. 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";
  2. As per my knowledge, Using this OAuth Code we need to get the Access token, Refresh token and expires_in in the response.
    I’m getting the following:

token_type which is bearer
Scope which is https://ads.microsoft.com/msads.manage
expires_in => 3600
ext_expires_in => 3600
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?

  1. I think we need to include offline_access in the scope, in order to get the refresh token in the response. However, I’m getting this exception
    “AADSTS70000: The request was denied because one or more scopes requested are unauthorized or expired. The user must first sign in and grant the client application access to the requested scope”.

Correct me If I’m wrong. Below is the screenshot.

174066-screenshot-from-2022-02-14-17-01-08.png

Thanks in advance for your help !

Microsoft Advertising API
Microsoft Advertising API
A Microsoft API that provides programmatic access to Microsoft Advertising to manage large campaigns or to integrate your marketing with other in-house systems.
424 questions
{count} vote

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.