gpt4 book ai didi

java - 如何在线创建和验证 Onedrive 或 Sharepoint 的 Webhook?

转载 作者:行者123 更新时间:2023-12-03 04:12:34 27 4
gpt4 key购买 nike

我想要订阅我的 OneDrive 文件夹。成功授权并收到 OneDrive 的访问 token 后,我可以使用 OneDrive 中的所有文件夹和文件。但我无法创建订阅。

我正在实现一个 Java Spring Boot 应用程序,该应用程序已部署到 Azure,并且可以通过例如https://{tenant}.westeurope.cloudapp.azure.com/api

When I try to create a subscription, I receive a response code: 400 with the message: "Validation request failed. Must respond with 200 OK to this request."

看完所有精彩documentation ,我找不到解决我的问题的方法。也许我误解了一些一般设置的事情。

这些是我的端点:

@RestController
@CrossOrigin
public class OneDriveSubscriptionsController {

@Autowired
private SubscriptionService subscriptionService;

// Should be used to create a subscription
@RequestMapping(method=RequestMethod.GET, value="/create")
public ResponseEntity<?> create() {

OneDriveSubscriptionVO subscription = new OneDriveSubscriptionVO();
oneDriveSubscriptionService.createSubscription(subscription);
return new ResponseEntity<String>("", HttpStatus.OK);
}

// Should be used to validate a subscription
@RequestMapping(method=RequestMethod.POST, value="/create")
public ResponseEntity<?> validation(@RequestParam String validationtoken) {

ResponseEntity<String> response = new ResponseEntity<String>(validationtoken, HttpStatus.OK);
return response;
}

// Should be used to receive notification's.
@RequestMapping(method=RequestMethod.POST, value="/notification" consumes=MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> subscriptionUpdate(@RequestBody OneDriveSubscriptionVO subscription) {
// working with received notifications.
}
}

我的服务类别:

@Service
public class SubscriptionService {

public synchronized String create(OneDriveSubscriptionVO subscription) {
String url = "https://graph.microsoft.com/v1.0/subscriptions";
String authorization = "Bearer "+AccessInformation.getAccess_token();
HttpClient httpclient = HttpClients.createDefault();
String ret = null;

try {
URIBuilder builder = new URIBuilder(url);
URI uri = builder.build();

String serialisedJsonString = Util.serializeJson(subscription);
StringEntity entity = new StringEntity(serialisedJsonString);

HttpPost request = new HttpPost(uri);
request.setEntity(entity);
request.setHeader("Authorization", authorization);
request.setHeader("Content-Type", "application/json");

HttpResponse response = httpclient.execute(request);
HttpEntity responseEntity = response.getEntity();

ret = EntityUtils.toString(responseEntity);

} catch (Exception e) {
logger.error(e.getMessage());
}

return ret;
}
}

我的助手类:

public class Util {
private <T>String serializeJson(T obj) {
Gson gson;
GsonBuilder builder = new GsonBuilder();
builder.setPrettyPrinting();
gson = builder.create();
return gson.toJson(obj);
}
}

我的 OneDriveSubscriptionVO 类(简化):

@JsonIgnoreProperties(ignoreUnknown = true)
public class OneDriveSubscriptionVO {

private String id;
private String clientState;
private String expirationDateTime;
private String notificationUrl;
private String resource;
private String changeType;

public OneDriveSubscriptionVO() {
this.clientState = "random string";
this.expirationDateTime = "2019-05-15T11:23:00.000Z";
this.notificationUrl= "https://{tenant}.westeurope.cloudapp.azure.com/api/notification";
this.resource = "/me/drive/root";
this.changeType = "updated";
}

据我所知,我必须使用 OneDriveSubscriptionVO 构造函数中的数据创建订阅。我必须在 5 秒内回复收到的验证 token 。

发送创建数据后,我收到错误“验证请求失败。必须对此请求响应 200 OK。”并且没有验证 token 来启用订阅。

这是我想用来创建订阅的 json:

{
"clientState": "random string",
"expirationDateTime": "2019-05-15T11:23:00.000Z",
"notificationUrl": "https://{tenant}.cloudapp.azure.com/onedrive/api/notification",
"resource": "/me/drive/root",
"changeType": "updated"
}

这是我收到的:

{
"error": {
"code": "InvalidRequest",
"message": "Subscription validation request failed. Must respond with 200 OK to this request.",
"innerError": {
"request-id": "5bf8a5e2-efd4-4863-bdc3-1fb7c89afa83",
"date": "2019-04-16T11:43:58"
}
}
}

我认为通常有问题,因为在我的“创建订阅 POST 请求”之后没有触发任何端点。

最佳答案

验证请求被发送到与通知相同的端点。看起来您希望针对 /create 进行验证,而您为通知指定的端点是 /notification。您需要将 POST-to-/create 的实现与 POST-to-/notification 合并。下面是一个示例 Azure 函数,可以让您了解它的样子:

#r "Newtonsoft.Json"
 
using System;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using Newtonsoft.Json;
 
public static async Task<object> Run(HttpRequestMessage request, TraceWriter log)
{
var content = await request.Content.ReadAsStringAsync();

var query = request.RequestUri.Query
.TrimStart('?')
.Split('&')
.Where(x => !String.IsNullOrEmpty(x))
.Select(x => x.Split('='))
.ToDictionary(x => x[0], x => Uri.UnescapeDataString(x[1]).Replace("+", " "), StringComparer.OrdinalIgnoreCase)
?? new Dictionary<string,string>();

string token;

if (query.TryGetValue("validationToken", out token))
{
var response = request.CreateResponse(HttpStatusCode.OK);
response.Content = new StreamContent(new MemoryStream(Encoding.UTF8.GetBytes(token)));
return response;
}
 
return request.CreateResponse(HttpStatusCode.OK, "Done!");
}

关于java - 如何在线创建和验证 Onedrive 或 Sharepoint 的 Webhook?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55709037/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com