簡體   English   中英

無法從 firebase 雲函數中的外部 api 讀取 POST 響應正文

[英]Cannot read POST response body from external api in firebase cloud functions

我有一個雲 function 向 LinkedIn API 發出 http POST 請求以檢索訪問令牌。 問題是我無法讀取響應的body ,它總是undefined

這是代碼

exports.linkedinToken = functions.https.onRequest((req, res) => {
    let url = "https://linkedin.com/oauth/v2/accessToken?...REQUIRED_PARAMS...";

    request.post(url, {json: true}, (err, response, body) => {
      if (!!err) { // this is never happening
        console.log(err);
        throw err;
      } else {
        console.log(response.body); // this is always undefined
        console.log(body); // this is always undefined
      }
});

如果我用 Postman 或 curl 嘗試相同的請求,它會完美運行,但我真的不明白為什么。

在此請求之前,我向linkedin API 發出了另一個GET 請求,它工作正常。 也許 POST 請求有問題。

我認為您缺少標題。

var request = require('request');
request.post({
  headers: {
    'content-type' : 'application/x-www-form-urlencoded',
    'cache-control' : 'no-cache'
  },
  url: url
}, function(error, response, body){
  console.log(body);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM