我一直在尝试遵循快速入门指南中概述的方法,但我一直坚持这一点。
https://docs.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=code&redirect_uri=" + bingRedirectUrl+ "&scope=https ://ads.microsoft.com/msads.manage";
据我所知,使用此 OAuth 代码,我们需要在响应中获取访问令牌、刷新令牌和 expires_in。但是,我得到以下信息:
- Token_type 是承载
- 范围是https://ads.microsoft.com/msads.manage
- expires_in => 3600
- 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;
}
我该如何解决这个问题?我是否完全发送不同的请求?
在此先感谢您的帮助 !