S. *_* E. 2 microsoft-graph-api
我尝试从 Microsoft Graph 请求访问令牌和刷新令牌,但将“offline_access”添加到范围会使范围无效。
这是一项服务,用户允许我们访问一次 Outlook 日历,之后该服务每 20 分钟检查一次他们的日历中是否有事件。在请求访问“User.Read”和“ Calendars.Read”时,我已设法获得同意并提取数据,但是当我添加“ offline_access”时,我收到消息The provided resource value for the input parameter 'scope' is not valid.
我有一个具有我想要的权限的数组
private static final String[] ACCESS_PERMISSIONS = {
"https://graph.microsoft.com/User.Read",
"https://graph.microsoft.com/Calendars.Read",
"https://graph.microsoft.com/offline_access",
};
Run Code Online (Sandbox Code Playgroud)
然后将它们组合并编码
String encodedScope = URLEncoder.encode(
Arrays.stream(ACCESS_PERMISSIONS)
.reduce(
(a,b) -> a + " " + b)
.get(), "UTF-8").replace("+","%20");
Run Code Online (Sandbox Code Playgroud)
结果是字符串https%3A%2F%2Fgraph.microsoft.com%2FUser.Read%20https%3A%2F%2Fgraph.microsoft.com%2FCalendars.Read%20https%3A%2F%2Fgraph.microsoft.com%2Foffline_access
然后我请求令牌
String appId = "client_id=" + clientID;
String scope = "&scope=" + encodedScope;
String authCode = "&code=" + code;
String redirect = "&redirect_uri=" + redirectUri;
String grantType = "&grant_type=authorization_code";
String secret = "&client_secret=" + clientSecret;
String data = appId + scope + authCode + redirect + grantType + secret;
// Create POST request
URL url = new URL(postUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// Configure request
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Accept", "application/json");
connection.setDoOutput(true);
// Send data
OutputStream os = connection.getOutputStream();
byte[] inputBytes = data.getBytes(ENCODING);
os.write(inputBytes, 0, inputBytes.length); // Bytes, Offset, Length
Run Code Online (Sandbox Code Playgroud)
最终的链接看起来像
https://login.microsoftonline.com/common/oauth2/v2.0/authorize?
client_id=[CLIENT ID]
&response_type=code
&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Fsport%2Fauth%2Foutlook
&response_mode=query
&scope=https%3A%2F%2Fgraph.microsoft.com%2FUser.Read%20https%3A%2F%2Fgraph.microsoft.com%2FCalendars.Read%20https%3A%2F%2Fgraph.microsoft.com%2Foffline_access
&state=2737
Run Code Online (Sandbox Code Playgroud)
我希望这会返回一个访问令牌和一个刷新令牌,但正如前面提到的,我收到一条消息,表明该范围的资源值无效。
该服务必须能够刷新令牌,因为它应该在没有干预的情况下运行大约一年。
范围不是https://graph.microsoft.com/offline_access,它只是offline_access。Offline Access 是一个特殊的 AAD 范围,它告诉它返回刷新令牌,它不是 Microsoft Graph 范围。
你实际上可以https://graph.microsoft.com/全面放弃。您需要指定 FQDN 的唯一时间是当您从应用程序注册请求默认范围集(即https://graph.microsoft.com/.default)时,但这通常仅在您使用仅应用程序/守护程序身份验证流程(客户端凭据)时使用。
| 归档时间: |
|
| 查看次数: |
3684 次 |
| 最近记录: |