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