如何使用我的应用程序脚本代码进行 CURL 调用
How do I make a CURL call using my app script codes
示例代码是这样给我的。
调用示例[=12=]
curl --location --request POST 'https://authentication.carfax.ca/oauth/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Cookie: did=s%3Av0%3Ad4c8e610-067c-11ec-84ad-
83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg;
did_compat=s%3Av0%3Ad4c8e610-067c-11ec-84ad-
83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg' \
--data-urlencode 'audience=https://api.carfax.ca' \
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode 'client_id=PROVIDEDBYINTEGRATIONSUPPORT' \
--data-urlencode 'client_secret=PROVIDEDBYINTEGRATIONSUPPORT'
我的应用程序脚本代码如下,其中 client_id 更改为我的实际客户端 ID,并且 client_secret 也已更新。我无法让这段代码工作。谁能建议我应该如何重写代码?
function myFunction() {
var url = 'https://authentication.carfax.ca/oauth/token';
var headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Cookie': 'did=s%3Av0%3Ad4c8e610-067c-11ec-84ad-83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg;did_compat=s%3Av0%3Ad4c8e610-067c-11ec-84ad-83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg',
"data-urlencode": {
'audience': 'https://api.carfax.ca',
'grant_type': 'client_credentials',
'client_id': 'xT18269vTVFk6Cy2QUjSyzVxs8BZcN6j',
'client_secret': '1Fz57F-lUuTZODoICC68ZUqSQoL6MCx4xR8PP9Bqfh6qGJThCC59uRZkJQF75HOK'
}
};
var response = UrlFetchApp.fetch(url, headers);
var text = response.getResponseCode();
Logger.log(headers);
}
我相信你的目标如下。
您想将以下 curl 命令转换为 Google Apps 脚本。
curl --location --request POST 'https://authentication.carfax.ca/oauth/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Cookie: did=s%3Av0%3Ad4c8e610-067c-11ec-84ad-
83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg;
did_compat=s%3Av0%3Ad4c8e610-067c-11ec-84ad-
83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg' \
--data-urlencode 'audience=https://api.carfax.ca' \
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode 'client_id=PROVIDEDBYINTEGRATIONSUPPORT' \
--data-urlencode 'client_secret=PROVIDEDBYINTEGRATIONSUPPORT'
这种情况请修改如下。
发件人:
var headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Cookie': 'did=s%3Av0%3Ad4c8e610-067c-11ec-84ad-83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg;did_compat=s%3Av0%3Ad4c8e610-067c-11ec-84ad-83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg',
"data-urlencode": {
'audience': 'https://api.carfax.ca',
'grant_type': 'client_credentials',
'client_id': 'xT18269vTVFk6Cy2QUjSyzVxs8BZcN6j',
'client_secret': '1Fz57F-lUuTZODoICC68ZUqSQoL6MCx4xR8PP9Bqfh6qGJThCC59uRZkJQF75HOK'
}
};
var response = UrlFetchApp.fetch(url, headers);
收件人:
var options = {
"headers": { 'Cookie': 'did=s%3Av0%3Ad4c8e610-067c-11ec-84ad-83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg;did_compat=s%3Av0%3Ad4c8e610-067c-11ec-84ad-83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg' },
"payload": {
'audience': 'https://api.carfax.ca',
'grant_type': 'client_credentials',
'client_id': 'xT18269vTVFk6Cy2QUjSyzVxs8BZcN6j',
'client_secret': '1Fz57F-lUuTZODoICC68ZUqSQoL6MCx4xR8PP9Bqfh6qGJThCC59uRZkJQF75HOK'
}
};
var response = UrlFetchApp.fetch(url, options);
注:
- 在您的 curl 命令中,使用了
Cookie
。在这种情况下,可能需要更新 cookie。如果出现错误,请检查这里。
- 如果出现状态码
403
这样的错误,我担心在这种情况下,Google端可能无法访问这个URL.
参考:
示例代码是这样给我的。
调用示例[=12=]
curl --location --request POST 'https://authentication.carfax.ca/oauth/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'Cookie: did=s%3Av0%3Ad4c8e610-067c-11ec-84ad-
83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg;
did_compat=s%3Av0%3Ad4c8e610-067c-11ec-84ad-
83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg' \
--data-urlencode 'audience=https://api.carfax.ca' \
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode 'client_id=PROVIDEDBYINTEGRATIONSUPPORT' \
--data-urlencode 'client_secret=PROVIDEDBYINTEGRATIONSUPPORT'
我的应用程序脚本代码如下,其中 client_id 更改为我的实际客户端 ID,并且 client_secret 也已更新。我无法让这段代码工作。谁能建议我应该如何重写代码?
function myFunction() {
var url = 'https://authentication.carfax.ca/oauth/token';
var headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Cookie': 'did=s%3Av0%3Ad4c8e610-067c-11ec-84ad-83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg;did_compat=s%3Av0%3Ad4c8e610-067c-11ec-84ad-83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg',
"data-urlencode": {
'audience': 'https://api.carfax.ca',
'grant_type': 'client_credentials',
'client_id': 'xT18269vTVFk6Cy2QUjSyzVxs8BZcN6j',
'client_secret': '1Fz57F-lUuTZODoICC68ZUqSQoL6MCx4xR8PP9Bqfh6qGJThCC59uRZkJQF75HOK'
}
};
var response = UrlFetchApp.fetch(url, headers);
var text = response.getResponseCode();
Logger.log(headers);
}
我相信你的目标如下。
您想将以下 curl 命令转换为 Google Apps 脚本。
curl --location --request POST 'https://authentication.carfax.ca/oauth/token' \ --header 'Content-Type: application/x-www-form-urlencoded' \ --header 'Cookie: did=s%3Av0%3Ad4c8e610-067c-11ec-84ad- 83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg; did_compat=s%3Av0%3Ad4c8e610-067c-11ec-84ad- 83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg' \ --data-urlencode 'audience=https://api.carfax.ca' \ --data-urlencode 'grant_type=client_credentials' \ --data-urlencode 'client_id=PROVIDEDBYINTEGRATIONSUPPORT' \ --data-urlencode 'client_secret=PROVIDEDBYINTEGRATIONSUPPORT'
这种情况请修改如下。
发件人:
var headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Cookie': 'did=s%3Av0%3Ad4c8e610-067c-11ec-84ad-83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg;did_compat=s%3Av0%3Ad4c8e610-067c-11ec-84ad-83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg',
"data-urlencode": {
'audience': 'https://api.carfax.ca',
'grant_type': 'client_credentials',
'client_id': 'xT18269vTVFk6Cy2QUjSyzVxs8BZcN6j',
'client_secret': '1Fz57F-lUuTZODoICC68ZUqSQoL6MCx4xR8PP9Bqfh6qGJThCC59uRZkJQF75HOK'
}
};
var response = UrlFetchApp.fetch(url, headers);
收件人:
var options = {
"headers": { 'Cookie': 'did=s%3Av0%3Ad4c8e610-067c-11ec-84ad-83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg;did_compat=s%3Av0%3Ad4c8e610-067c-11ec-84ad-83a048983dd7.IfTEs0EGLJ7r5kYxspPDGaSaw9fs2WPxYv5K%2FJv6Rpg' },
"payload": {
'audience': 'https://api.carfax.ca',
'grant_type': 'client_credentials',
'client_id': 'xT18269vTVFk6Cy2QUjSyzVxs8BZcN6j',
'client_secret': '1Fz57F-lUuTZODoICC68ZUqSQoL6MCx4xR8PP9Bqfh6qGJThCC59uRZkJQF75HOK'
}
};
var response = UrlFetchApp.fetch(url, options);
注:
- 在您的 curl 命令中,使用了
Cookie
。在这种情况下,可能需要更新 cookie。如果出现错误,请检查这里。 - 如果出现状态码
403
这样的错误,我担心在这种情况下,Google端可能无法访问这个URL.