如何通过Azure上传小文件API在Yammer API上上传文件



如何上传附件以及Yammer消息?

通过attachment1/messages.json端点域的任何遗留方法将不再工作。

新方法没有很好地记录:https://developer.yammer.com/docs/upload-files-into-yammer-groups

我在这里给出一个PHP的例子,但你可以在任何语言中做同样的事情。

你必须分两部分完成

  1. 首先将图片上传到https://filesng.yammer.com/v4/uploadSmallFile并获取图片的id。
  2. 将您的消息与新获得的图片id一起发送到https://www.yammer.com/api/v1/messages.json。

注意:这里我将使用Guzzle库进行REST调用

1。将图片发送到Azure云

protected function yammerFileUpload(string $file, string $filename): int
{
$multipart = [
[
'name'      => 'network_id',
'contents'  => $this->networkId,
],
[
'name'      => 'group_id',
'contents'  => $this->groupId,
],
[
'name'      => 'target_type',
'contents'  => 'GROUP',
],
[
'name'      => 'filename',
'contents'  => $filename,
],
[
'name'      => 'file',
'contents'  => $file,
'filename'  => $filename,
'headers'   => ['Content-Type' => 'image/jpeg']
],
];
$client = new Client();
$options = [
'headers'       => [
'Accept'        => 'application/json',
'Authorization' => "Bearer $this->yammerToken",
],
'multipart'     => $multipart,
];

$response = $client->request('POST', 'https://filesng.yammer.com/v4/uploadSmallFile', $options);
return json_decode((string)$response->getBody(), true)['id'];
}

当然,你必须用你自己的变量替换类变量。内容类型

2。发送你的消息

public function postMessage(string $message, string $file): array
{
$fileId = $this->yammerFileUpload($file, 'my-file.jpg');
$client = new Client();
$options = [
'headers'   => [
'Accept'        => 'application/json',
'Authorization' => "Bearer $this->token",
],
'form_params' => [
'body'               => $message,
'group_id'           => $this->groupId,
'attached_objects[]' => "uploaded_file:$fileId",
],
];
$response = $client->request('POST', 'https://www.yammer.com/api/v1/messages.json', $options);
return json_decode((string)$response->getBody(), true);
}