首页 > 解决方案 > Google Apps 脚本中的 PayPal REST API 错误 400

问题描述

我正在尝试使用 PayPal API为我的OnlyFans应用程序创建订阅。
我使用与使用 Postman 时完全相同的身体。
不知何故,我可以使用 Postman 获得响应,但尝试通过应用脚本执行此操作时遇到此错误。
我已经对任何尾随逗号等进行了三重四重检查。
似乎在格式中找不到任何错误。

为什么我收到错误代码 400?
"name":"INVALID_REQUEST","message":"Request is not well-formed, syntactically incorrect...


function restCall (){
  
  var restEndpoint = "https://api-m.sandbox.paypal.com/v1/billing/subscriptions"
  
  var head = {
    "Authorization":"Bearer "+ TokenProperties.getAccessToken(),
    "Content-Type": "application/json",
    "PayPal-Request-Id": "1111-1111-1111-1111"
  }

  var postPayload = 
  {
    "plan_id": "P-09E19660L3543673CMETTBBA",
    "subscriber": {
      "name": {
        "given_name": "John",
        "surname": "Doe"
      },
      "email_address": "sb-y9erz7310462@personal.example.com"
    },
    "application_context": {
      "brand_name": "Test",
      "user_action": "SUBSCRIBE_NOW",
      "payment_method": {
        "payer_selected": "PAYPAL",
        "payee_preferred": "IMMEDIATE_PAYMENT_REQUIRED"
      },
      "return_url": "https://example.com/returnUrl",
      "cancel_url": "https://example.com/cancelUrl"
    }
  }

  var params = {
    headers:  head,
    method: "POST",
    payload : postPayload
  }

  var response = UrlFetchApp.fetch(restEndpoint, params); 
}

标签: google-apps-scriptpaypalpaypal-sandboxrestpaypal-rest-sdk

解决方案


尝试

  var options = {
    "contentType": "application/json",
    "method": "post",
    "headers": head,
    "payload": JSON.stringify(postPayload)
  };

推荐阅读