【问题标题】:How to upload files on Yammer API via the Azure Upload Small File API如何通过 Azure 上传小文件 API 在 Yammer API 上上传文件
【发布时间】:2021-02-05 12:05:10
【问题描述】:

如何将附件与 Yammer 消息一起上传?

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

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

我在下面给出一个 PHP 的例子,但是你可以用任何语言做同样的事情。

【问题讨论】:

    标签: php file-upload guzzle yammer yammer-api


    【解决方案1】:

    你必须分两部分完成

    1. 先将图片上传到https://filesng.yammer.com/v4/uploadSmallFile,并获取图片的id。
    2. 像往常一样将您的消息发送到https://www.yammer.com/api/v1/messages.json,并附上新获得的图片ID。

    注意:我将在此处使用 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);
    }
    

    【讨论】:

    • 这太好了,Erdal,谢谢分享。您是如何获得访问令牌的?似乎需要 AAD 访问令牌,而开发人员令牌不起作用。您是否使用了基于浏览器的 oauth 流程?
    • 好的,我想出了别的办法。有些 Yammer 网络使用 SharePoint 进行文件存储,有些则不使用。那些不这样做的,旧的开发者令牌仍然有效。
    • @DerekGusoff 实际上我使用了 OAuth 流用户端(然后是服务器访问),根据文档,无论如何您都必须进行浏览器身份验证。然后我就使用了我从 API 获得的不记名令牌,并且它起作用了。
    猜你喜欢
    • 1970-01-01
    • 2013-09-01
    • 2021-07-22
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    • 2011-10-18
    • 2012-11-23
    • 1970-01-01
    相关资源
    最近更新 更多