java - Microsoft Graph:请求扩展返回 http 400 错误请求
问题描述
我在日历中的事件中添加了一个开放扩展,并试图将其读回。
这是网址:
https://graph.microsoft.com/v1.0/users/{userid}/calendars/{calendarId}=/events?$expand=Extensions($filter=Id eq 'c.i.m.p.server.entities.outlook.Event')
我无法让它在 Java 程序中工作。以下组合确实有效:
- 如果我删除 $expand... 参数,它可以运行我的 Java 程序。我也可以要求某些领域,这也有效。
- 该请求在 Postman 中有效(我只需要设置令牌)
- 当我以日历的所有者身份登录时,该请求在 Graph Explorer 中有效
这是我使用 Postman 阅读事件时的扩展名(在其中一个事件中)。这是事件中的最后一项:
"extensions@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('{userid}')/calendars('{calendarId}')/events('{eventId})/extensions",
"extensions": [
{
"@odata.type": "#microsoft.graph.openTypeExtension",
"id": "Microsoft.OutlookServices.OpenTypeExtension.c.i.m.p.server.entities.outlook.Event",
"extensionName": "c.i.m.p.server.entities.outlook.Event",
"adherentId": "12346",
"timeSlotID": "346463"
}
]
这是 Java 代码(Java 8,使用 java.io 和 java.net 库):
private static void doSomething(String _accessToken) throws IOException {
String urlString = "https://graph.microsoft.com/v1.0/users/{userId}/calendars/{calendarId}/events?$expand=Extensions($filter=Id eq 'c.i.m.p.server.entities.outlook.Event')";
URL url = new URL(urlString);
Proxy webProxy
= new Proxy(Proxy.Type.HTTP, new InetSocketAddress({proxy-address}, {port}));
HttpURLConnection connection = (HttpURLConnection) url.openConnection(webProxy);
// Set the appropriate header fields in the request header.
connection.setRequestProperty("Authorization", "Bearer " + _accessToken);
connection.setRequestProperty("Accept", "application/json");
connection.setDoOutput(true);
connection.setReadTimeout(5000);
connection.setRequestMethod(HttpMethod.GET);
try {
connection.connect();
int responseCode = connection.getResponseCode();
System.out.println("execute(), response code = " + responseCode);
String responseMessage = connection.getResponseMessage();
System.out.println("execute(), response Message = " + responseMessage);
String responseString = null;
try {
InputStream ins = connection.getInputStream();
BufferedReader br=new BufferedReader(new InputStreamReader(ins));
StringBuffer sb=new StringBuffer();
String line;
while ((line=br.readLine()) != null) {
sb.append(line);
}
responseString = sb.toString();
} catch (Exception e) {
System.out.println("Could not get input stream from response, error is " + e.toString());
}
System.out.println("execute(), httpResult = " + responseString);
} catch (IOException e) {
System.out.println(".execute(), IOException : " + e.toString());
} finally {
connection.disconnect();
}
}
我该如何解决?谢谢!
解决方案
400 表示错误的请求。这可能是因为 url 编码。Url 对查询字符串进行编码。
就像是
String query = "Extensions($filter=Id eq 'c.i.m.p.server.entities.outlook.Event'";
String url = "https://graph.microsoft.com/v1.0/users/{userId}/calendars/{calendarId}/events?
$expand=" + URLEncoder.encode(query, StandardCharsets.UTF_8.name());
或者,您可以根据需要使用图形服务 java api,这将有助于为您抽象所有交互,或者您可以使用任何可用的其余客户端。
推荐阅读
- c# - How to select a value from an angular dropdown in C# with selenium
- javascript - 在具有绝对位置的 div 中实现平滑过渡
- java - Java,Maven + Jenkinsfile - 找不到类路径
- javascript - How to implement custom Error class in typescript?
- javascript - 像 ookla 一样计算下载速度
- php - Elasticsearch Mapping Types in PHP
- django - How to display parameter from URL to template in class based views in Django
- javascript - capture regex group occurrence in a string
- kubernetes - 即使配置了 RBAC 策略,secrets 也可以被视为环境变量
- java - 由 3rd 方依赖项发出的出站请求的 URL 重写