如何通过Azure上传小文件API在Yammer API上上传文件 [英] How to upload files on Yammer API via the Azure Upload Small File API

查看:54
本文介绍了如何通过Azure上传小文件API在Yammer API上上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过/messages.json 端点的 attachment1 等字段的任何旧方法将不再起作用.

Any legacy method through attachment1 etc. fields of the /messages.json endpoint won't work anymore.

该新方法的文档记录不充分: https://developer.yammer.com/docs/upload-files-into-yammer-groups

The new method is not so well documented: https://developer.yammer.com/docs/upload-files-into-yammer-groups

我在下面用PHP举例说明,但是您可以使用任何语言进行同样的操作.

I'm giving here an example in PHP below, but you can do the same in any language.

推荐答案

您必须分两部分进行

  1. 首先将图片上传到 https://filesng.yammer.com/v4/uploadSmallFile并获取图片的ID.
  2. 将邮件照常发送到 https://www.yammer.com/api/v1/messages.json 以及刚获得的图片ID.
  1. First upload the picture to https://filesng.yammer.com/v4/uploadSmallFile and get the picture's id.
  2. Send your message as usual to https://www.yammer.com/api/v1/messages.json along with the freshly got picture id.

注意:我将在此处使用 Guzzle 库进行REST调用

Note: I will use here the Guzzle library for the REST calls.

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'];
}

当然,您必须用自己的替换类变量.而内容类型则是文件的类型.

Of course, you have to replace class variables with your owns. And the content type by your file's one.

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);
}

这篇关于如何通过Azure上传小文件API在Yammer API上上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆