问题描述
如何上传附件和 Yammer 消息?
任何通过 attachment1
等字段的 /messages.json
端点的旧方法将不再有效。
新方法没有很好的文档说明:https://developer.yammer.com/docs/upload-files-into-yammer-groups
我在下面给出了一个 PHP 示例,但您可以使用任何语言执行相同的操作。
解决方法
你必须分两部分做
- 首先将图片上传到 https://filesng.yammer.com/v4/uploadSmallFile 并获取图片的 id。
- 像往常一样将您的消息连同新获得的图片 ID 发送给 https://www.yammer.com/api/v1/messages.json。
注意:我将在此处使用 Guzzle 库进行 REST 调用。
1.将图片发送到蔚蓝云
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',true);
}