特创数字科技 2023-02-25 16:10 采纳率: 83.3%
浏览 22
已结题

unicloud云函数请求抖音文本安全检测接口,结果显示错误

unicloud云函数请求抖音文本安全检测接口,结果显示错误 如何解决
可以的话需要详细的代码
unicloud云函数代码

'use strict'; 
// 云函数入口文件 
const cloud = require('douyin-sdk') 
const db = uniCloud.database()
exports.main = async (event, context) => { 
console.log('event : ', event) 
const Tokin = event.queryStringParameters.Tokin; 
const url = 'https://developer.toutiao.com/api/v2/tags/text/antidirt'; 
console.log('Tokin : ', Tokin) 
//通过appid和secret请求到抖音的 
const result = await uniCloud.httpclient.request('https://developer.toutiao.com/api/v2/tags/text/antidirt',{ 
    method:'POST', 
    headers: { 
        'X-Token': Tokin, 
        'Content-Type': 'application/json', 
         'type': 'string',
        }, 
        body: JSON.stringify({ 'tasks' : [ {'content' : '你好'} ] }), 
        dataType: 'JSON' ,
        }); 
        console.log("抖音用户openid:" + result.data.data) 
        const resultt = JSON.parse(result.data)
         return { 
             Tokin, 
             resultt, 
             result, 
             url
             } 
             };

返回的结果

result:
data:
data: (153) [123, 34, 101, 114, 114, 111, 114, 95, 105, 100, 34, 58, 32, 34, 54, 55, 57, 53, 57, 53, 99, 101, 52, 56, 102, 97, 52, 51, 97, 57, 98, 50, 51, 101, 57, 49, 48, 48, 53, 48, 52, 97, 101, 98, 100, 56, 34, 44, 32, 34, 99, 111, 100, 101, 34, 58, 32, 52, 48, 48, 44, 32, 34, 109, 101, 115, 115, 97, 103, 101, 34, 58, 32, 34, 39, 116, 97, 115, 107, 115, 39, 32, 105, 115, 32, 97, 32, 114, 101, 113, 117, 105, 114, 101, 100, 32, 112, 114, 111, 112, ]
type: "Buffer"
__proto__: Object
headers: {server: "Tengine", content-type: "application/json", content-length: "153", connection: "keep-alive", date: "Sat, 25 Feb 2023 08:00:59 GMT", }
res: {status: 400, statusCode: 400, statusMessage: "Bad Request", headers: {}, size: 153, }
status: 400
__proto__: Object

  • 写回答

1条回答 默认 最新

  • MarkHan_ 2023-02-25 16:23
    关注

    该回答引用GPTᴼᴾᴱᴺᴬᴵ
    根据你提供的代码和结果,你的云函数请求抖音文本安全检测接口出错的原因可能是请求参数格式不正确。具体来说,请求参数中的type应该是放在headers中的而不是body中的。以下是修正后的云函数代码:

    'use strict'; 
    // 云函数入口文件 
    const db = uniCloud.database()
    
    exports.main = async (event, context) => { 
      console.log('event : ', event) 
      const Tokin = event.queryStringParameters.Tokin; 
      const url = 'https://developer.toutiao.com/api/v2/tags/text/antidirt'; 
      console.log('Tokin : ', Tokin) 
    
      // 通过appid和secret请求到抖音的 
      const result = await uniCloud.httpclient.request(url,{ 
        method:'POST', 
        headers: { 
          'X-Token': Tokin, 
          'Content-Type': 'application/json', 
        }, 
        body: JSON.stringify({ 'tasks' : [ {'content' : '你好'} ] }), 
        dataType: 'JSON' ,
        type: 'string',
      }); 
    
      console.log("抖音用户openid:" + result.data.data) 
      const resultt = JSON.parse(result.data)
      return { 
        Tokin, 
        resultt, 
        result, 
        url
      } 
    };
    
    
    

    主要的修改是将请求参数type从body中移到headers中。另外,请求的url也放到了一个变量url中方便管理。请注意,该代码仅为参考,实际使用时需要根据自己的业务逻辑进行适当的修改。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 2月28日
  • 创建了问题 2月25日