1. 首页 > 游戏新闻

微信小程序连接度娘ai 微信小程序连接服务器失败是什么意思

作者:admin 更新时间:2025-12-24
摘要:微信小程序连接百度AI可以通过以下步骤实现: 注册百度AI平台 访问百度AI开放平台(https://ai.baidu.com/)。 注册并登录,创建应用,获取API Key和Secret Key。 小程序环境准备 在微信小程序开发者工具中创建一个...,微信小程序连接度娘ai 微信小程序连接服务器失败是什么意思

 

微信小程序连接度娘AI可以通过下面内容流程实现:

注册度娘AI平台

  1. 访问度娘AI放开平台(https://ai.baidu.com/)。
  2. 注册并登录,创建应用,获取API Key和Secret Key。

小程序环境准备

  1. 在微信小程序开发者工具中创建壹个新的小程序项目。
  2. app.js中配置度娘AI的API Key和Secret Key。
App({
  onLaunch: function () {
    // 配置度娘AI的API Key和Secret Key
    this.setBaiduAIConfig();
  },
  setBaiduAIConfig: function () {
    const that = this;
    wx.request({
      url: 'https://aip.baidubce.com/oauth/2.0/token',
      data: {
        grant_type: 'client_credentials',
        client_id: '你的API Key',
        client_secret: '你的Secret Key'
      },
      success: function (res) {
        that.globalData.baiduAIConfig = res.data;
      }
    });
  },
  globalData: {
    baiduAIConfig: {}
  }
});

调用度娘AI接口

在需要调用度娘AI接口的页面中,运用下面内容方式获取access_token,并调用相应的API。

Page({
  onLoad: function () {
    this.getAccessToken();
  },
  getAccessToken: function () {
    const that = this;
    const baiduAIConfig = getApp().globalData.baiduAIConfig;
    wx.request({
      url: 'https://aip.baidubce.com/oauth/2.0/token',
      data: {
        grant_type: 'client_credentials',
        client_id: baiduAIConfig.client_id,
        client_secret: baiduAIConfig.client_secret
      },
      success: function (res) {
        that.accessToken = res.data.access_token;
        that.callBaiduAI();
      }
    });
  },
  callBaiduAI: function () {
    const that = this;
    wx.request({
      url: 'https://aip.baidubce.com/rest/2.0/xxx/xxx', // 替换为相应的API URL
      data: {
        access_token: that.accessToken,
        // 其他参数...
      },
      success: function (res) {
        // 处理返回结局
      }
    });
  }
});

权限申请

根据你调用的度娘AI接口,也许需要在小程序中申请相应的权限,具体权限申请方法请参考微信小程序官方文档。

测试

完成以上流程后,可以在微信开发者工具中运行小程序,测试度娘AI接口调用是否成功。

这样,你就可以在微信小程序中连接并运用度娘AI接口了。