【问题标题】:Podio API addItem callPodio API addItem 调用
【发布时间】:2013-02-15 21:54:57
【问题描述】:

我正在尝试在 nodejs 模块中实现 https://developers.podio.com/doc/items/add-new-item-22362 Podio API addItem 调用。这是代码:

var _makeRequest = function(type, url, params, cb) {
  var headers = {};
  if(_isAuthenticated) {
    headers.Authorization = 'OAuth2 ' + _access_token ;
  }
  console.log(url,params);
  _request({method: type, url: url, json: true, form: params, headers: headers},function (error, response, body) {
    if(!error && response.statusCode == 200) {
      cb.call(this,body);
    } else {
      console.log('Error occured while launching a request to Podio: ' + error + '; body: ' + JSON.stringify (body));
    }
  });
}

exports.addItem = function(app_id, field_values, cb) {
  _makeRequest('POST', _baseUrl + "/item/app/" + app_id + '/',{fields: {'title': 'fgdsfgdsf'}},function(response) {
    cb.call(this,response);
  });

它返回以下错误:

{"error_propagate":false,"error_parameters":{},"error_detail":null,"error_description":"No matching operation could be found. No body was given.","error":"not_found"}

应用程序中只需要“title”属性 - 我在 Podio GUI 中检查过。我还尝试从我发布到的 url 中删除尾部斜杠,然后发生类似的错误,但在错误描述中找不到 URL 消息。

我将设置一个代理来捕获原始请求,但也许有人只是看到代码中的错误?

感谢任何帮助。

【问题讨论】:

    标签: node.js api podio


    【解决方案1】:

    没关系,我找到了解决方案。问题是 addItem 调用是我的第一个“真正的”-API 方法实现,主体中带有 JSON 参数。之前的调用是 authentication 和 getApp,它是 GET 并且没有任何参数。

    问题是 Podio 支持 POST 键值对进行身份验证,但不支持所有调用,我试图对所有调用使用单个 _makeRequest() 方法,包括 auth 和 real-API那些。

    看起来我需要为身份验证实现一个,为所有 API 调用实现一个。

    无论如何,如果有人需要在节点上调用 addItem 的工作概念证明,那就是(假设你事先有一个身份验证令牌)

    _request({method: 'POST', url: "https://api.podio.com/item/app/" + app_id + '/', headers: headers, body: JSON.stringify({fields: {'title': 'gdfgdsfgds'}})},function(error, response, body) {
      console.log(body);
    });
    

    【讨论】:

      【解决方案2】:
      • 您应该将 content-type 设置为 application/json
      • 将正文作为字符串化 json 发送。

        const getHeaders = async () => {
          const headers = {
            Accept: 'application/json',
            'Content-Type': 'application/json; charset=utf-8',
          };
        
          const token = "YOUR APP TOKEN HERE";
          headers.Authorization = `Bearer ${token}`;
        
          return headers;
        }
        
        
        const createItem = async (data) => {
            const uri = `https://api.podio.com/item/app/${APP_ID}/`;
            const payload = {
                fields: {
                  [data.FIELD_ID]: [data.FIELD_VALUE],
                },
            };
            const response = await fetch(uri, {
                method: 'POST',
                headers: await getHeaders(),
                body: JSON.stringify(payload),
            });
        
            const newItem = await response.json(); 
            return newItem;
        }
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多