Reputation: 5577
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.
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
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
This is a sample of my code:
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());
}
The response is always the same:
error:"invalid_request",
error_description:"The grant type was not specified in the request"
I have also tried:
var dataString = {
"grant_type": "client_credentials",
"scope" : "basic user"
};
But the response does not change. What am I doing wrong?
UPDATE 2
I made the following changes:
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());
}
Now, I get this error:
error:"invalid_client"
error_description:"Client credentials were not found in the headers or body"
Upvotes: 1
Views: 4418
Reputation: 201378
How about following sample script? I don't know whether this works fine, because I don't have <API USERNAME>
and <API SECRET KEY>
.
Sample script :
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.
Upvotes: 3