简体   繁体   English

无法从 firebase 云函数中的外部 api 读取 POST 响应正文

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

I have a cloud function which makes an http POST request to the LinkedIn API in order to retrieve an access token.我有一个云 function 向 LinkedIn API 发出 http POST 请求以检索访问令牌。 The problem is that I cannot read the body of the response, it's always undefined .问题是我无法读取响应的body ,它总是undefined

Here's the code这是代码

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
      }
});

If I try the same request with Postman or curl it works perfectly, and I cannot really understand why.如果我用 Postman 或 curl 尝试相同的请求,它会完美运行,但我真的不明白为什么。

Before this request, I made another GET request to the linkedin API and it works normally.在此请求之前,我向linkedin API 发出了另一个GET 请求,它工作正常。 Maybe there's a problem with POST requests.也许 POST 请求有问题。

I think you are missing headers.我认为您缺少标题。

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.

相关问题 Dialogflow + 外部 API + Google Cloud Functions *无 * Firebase:如何返回履行响应? - Dialogflow + external API + Google Cloud Functions *without* Firebase: how to return fulfillment response? 从Cloud Functions for Firebase读取数据? - Read data from Cloud Functions for Firebase? 使用 Firebase Cloud Functions,我可以从外部 API 安排更新以更新 Firestore - Using Firebase Cloud Functions, can I schedule updates from an external API to update Firestore 对 Firebase Cloud Functions (Spark Tier) 中外部 API 的 HTTP 请求被拒绝 - HTTP request to an external API in Firebase Cloud Functions (Spark Tier) refused firebase函数和外部API - firebase functions and external API Firebase 云函数。 无法读取未定义 (.ref) 的属性父级 - Firebase Cloud Functions. Cannot read property parent of undefined (.ref) firebase云函数无法读取未定义的属性“ ref” - firebase cloud functions Cannot read property 'ref' of undefined 带有 Firebase 云功能的 Axios 帖子 - Axios post with firebase cloud functions Firebase的Cloud Functions:如何从数据库同步读取数据? - Cloud Functions for Firebase: how to read data from Database synchronously? 如何从 Cloud Functions for Firebase 文件夹读取证书文件 - How to read cert files from Cloud Functions for Firebase Folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM