无法获取刷新令牌-必应广告API



我一直在尝试遵循《快速入门》指南中概述的方法,但我一直坚持下去。

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

必应广告API版本:13.0.13

成功获取授权码

获取Oauth代码的URL:"https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id="+bingClientId.trim((+"&response_type=代码&redirect_uri="+bingRedirectUrl+"&范围=https://ads.microsoft.com/msads.manage";

据我所知,使用这个OAuth代码,我们需要在响应中获得Access令牌、Refresh令牌和expires_in。但是,我得到了以下内容:

  1. 作为承载的Token_type
  2. 范围https://ads.microsoft.com/msads.manage
  3. expires_in=>3600
  4. ext_expires_in=>3600

5.access_token

很遗憾,我无法获取刷新令牌。以下是我的代码:

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;
}

如何解决此问题?我是否发送了完全不同的请求?

提前感谢您的帮助!

在同意请求的作用域参数中包含offline_access。

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

最新更新