getTempMedia.md 4.59 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
小程序客服 /客服消息 /获取客服消息内的临时素材

# 获取客服消息内的临时素材

> 接口应在服务器端调用,详细说明参见[服务端API](https://developers.weixin.qq.com/miniprogram/dev/framework/server-ability/backend-api.html)。

> 本接口支持[云调用](https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/openapi/openapi.html)。需开发者工具版本 >= `1.02.1904090`(最新[稳定版下载](https://developers.weixin.qq.com/miniprogram/dev/devtools/stable.html)),`wx-server-sdk` >= `0.4.0`

## 接口说明

### 接口英文名

getTempMedia

### 功能描述

该接口用于获取客服消息内的临时素材。即下载临时的多媒体文件。目前小程序仅支持下载图片文件

### 注意事项

如果调用成功,会直接返回图片二进制内容,如果请求失败,会返回 JSON 格式的数据。

## 调用方式

### HTTPS 调用

```text
GET https://api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN 
```

### 云调用

- 出入参和 HTTPS 调用相同,调用方式可查看[云调用说明文档](https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/openapi/openapi.html)
- 接口方法为: openapi.customerServiceMessage.getTempMedia

### 第三方调用

- 调用方式以及出入参和 HTTPS 相同,仅是调用的 token 不同
- 该接口所属的权限集 id 为:1、3、8、9、11、19、30、31、59、61、62、100
- 服务商获得其中之一权限集授权后,可通过使用[authorizer_access_token](https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/api_authorizer_token.html)代商家进行调用

### 请求参数

| 属性                                  | 类型   | 必填 | 说明                                                         |
| :------------------------------------ | :----- | :--- | :----------------------------------------------------------- |
| access_token / cloudbase_access_token | string | 是   | 接口调用凭证,该参数为 URL 参数,非 Body 参数。access_token和cloudbase_access_token二选一 其中access_token可通过[getAccessToken](https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html)接口获得; 如果是第三方代调用请传入[authorizer_access_token](https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/api_authorizer_token.html); cloudbase_access_token可通过[getOpenData](https://developers.weixin.qq.com/miniprogram/dev/wxcloudrun/src/guide/weixin/token.html)接口获得 |
| media_id                              | string | 是   | 媒体文件 ID。可通过 uploadTempMedia 接口获得media_id         |

### 返回参数

| 属性    | 类型   | 说明                    |
| :------ | :----- | :---------------------- |
| Buffer  | Buffer | 成功时返回的图片 Buffer |
| errcode | number | 失败时返回错误码        |
| errmsg  | string | 失败时返回错误信息      |

## 调用示例

> 示例说明: CURL请求失败示例

### 请求数据示例

```text
curl -I -G "https://api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID" 
```

### 返回数据示例

```json
{
  "errcode": 40007,
  "errmsg": "无效媒体文件 ID"
}
 
```

> 示例说明: 云函数调用

### 请求数据示例

```text
const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV,
})
exports.main = async (event, context) => {
  try {
    const result = await cloud.openapi.customerServiceMessage.getTempMedia({
        "mediaId": ''
      })
    return result
  } catch (err) {
    return err
  }
} 
```

### 返回数据示例

```json
{
  "errCode": 0,
  "errMsg": "openapi.customerServiceMessage.getTempMedia:ok",
   "contentType": "image/jpeg",
   "buffer": "Buffer"
} 
```

### 错误码

| 错误码 | 错误码取值                                              | 解决方案                                                     |
| :----- | :------------------------------------------------------ | :----------------------------------------------------------- |
| -1     | system error                                            | 系统繁忙,此时请开发者稍候再试                               |
| 40001  | invalid credential access_token isinvalid or not latest | 获取 access_token 时 AppSecret 错误,或者 access_token 无效。请开发者认真比对 AppSecret 的正确性,或查看是否正在为恰当的公众号调用接口 |

|      |      |      |
| :--- | :--- | :--- |
|      |      |      |
|      |      |      |
|      |      |      |