Google Apps脚本和curl [英] Google Apps Script and curl

查看:123
本文介绍了Google Apps脚本和curl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个非常简单的问题,但是对于 curl 我还是很陌生,并且从未对其进行过正确的解释。这是我第一次必须通过 curl 获取访问令牌,而我从未见过这样的示例。我的Google搜索没有帮助。

This is probably a very simple problem, but I am pretty new to curl and have never had it properly explained. This is the first time I have had to get an access token via curl and I have never seen an example. My google searches have not been helpful.

我知道一旦获得访问令牌,如何提出交易请求,但无法获得令牌以继续进行交易步骤。

I know how to make the transaction requests once I have the access token, but can't get the token to move on to that step.

以下是如何获取令牌的api文档示例:

Below is the api documentation example of how to get the token:

curl -X POST -u <API USERNAME>:<API SECRET KEY> https://api.neverbounce.com/v3/access_token\
-d grant_type=client_credentials\
-d scope=basic+user

由于 UrlFetchApp <,我应该如何在 Google Apps脚本中进行此调用/ code>需要一个URL,但是此示例中的URL是请求的一部分吗?

How am I supposed to make this call in Google Apps Script since UrlFetchApp requires a url, but the url in this example is part of the request?

UPDATE

这是我的代码示例:

function NBpost() {
  var dataString = 'grant_type=client_credentials&scope=basic+user';
  var url = "https://api.neverbounce.com/v3/access_token";
  var options =
    {
      "method" : "post",
      "auth": {
        'user': <USERNAME>,
        'pass': <SECRET KEY>,
      },
      body : dataString,
      "muteHttpExceptions" : true
    };
  var response = JSON.parse(UrlFetchApp.fetch(url, options).getContentText());
}

响应始终是相同的:

error:"invalid_request", 
error_description:"The grant type was not specified in the request"

我也尝试过:

var dataString = {
  "grant_type": "client_credentials",
  "scope" : "basic user"
};

但响应不会改变。我在做什么错?

But the response does not change. What am I doing wrong?

更新2

我做了以下更改:

function myFunction(){
    var url ='https://api.neverbounce.com/v3/access_token';
    var options = {
      method: "post",
      u: {
        <USERNAME> : <SECRET KEY>
      },
      payload: {
        "grant_type": "client_credentials",
        "scope": "basic user"
      },
      muteHttpExceptions: true
    };
    var response = JSON.parse(UrlFetchApp.fetch(url, options).getContentText());
}

现在,我收到此错误:

error:"invalid_client"
error_description:"Client credentials were not found in the headers or body"


推荐答案

下面的示例脚本怎么样?我不知道这是否行得通,因为我没有< API USERNAME> < API SECRET KEY>

How about following sample script? I don't know whether this works fine, because I don't have <API USERNAME> and <API SECRET KEY>.

示例脚本:

var apiusername = <API USERNAME>
var apisecretkey = <API SECRET KEY>
var url ='https://api.neverbounce.com/v3/access_token';
var options = {
  method: 'post',
  headers : {"Authorization" : " Basic " + Utilities.base64Encode(apiusername + ":" + apisecretkey)},
  payload: {
    "grant_type": "client_credentials",
    "scope": "basic+user"
  },
  muteHttpExceptions: true
};
var response = JSON.parse(UrlFetchApp.fetch(url, options).getContentText());

如果这不起作用或我误解了您的问题,对不起。

If this doesn't work or if I misunderstand your question, I'm sorry.

这篇关于Google Apps脚本和curl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆