Unable to exchange `code` for Salesforce Marketing Cloud token - salesforce-marketing-cloud

I am trying to exchange a Salesforce Marketing Cloud (SFMC) authorization_code for an access_token per the docs here: https://developer.salesforce.com/docs/atlas.en-us.mc-app-development.meta/mc-app-development/access-token-app.htm
curl https://{my_subdomain}.auth.marketingcloudapis.com/v2/token \
--request POST \
--header "Content-Type: application/json" \
--data '{"grant_type": "authorization_code", "code": "{{my_code}}", "client_id": "{{my_client_id}}", "redirect_uri": "https://127.0.0.1:80/", “client_secret: “{{my_client_secret}}”, "scope": "audiences_read list_and_subscribers_write offline"}'
Yet it continually gives me the following error:
{"documentation":"https://developer.salesforce.com/docs/atlas.en-us.mc-apis.meta/mc-apis/error-handling.htm","errorcode":0,"message":"Bad Request"}%
I have no idea where I am going wrong, any help is appreciated.
I was seeing "correct" errors when I was using an expired code and when my client_id and/or client_secret were wrong, but nothing after I pasted in the correct values. Perhaps it is my scopes?
According to the docs linked in the error code, The token was not found in the request, or it is invalid or expired.. This is the only error code that has customcode == 0. It is strange since I am trying to get a token, not pass one in, and that error code is associated with a 401, which may or may not be what I am receiving back.

It turns out my client_secret in my data was using the incorrect double quote. It was a slanted double-quote. I discovered this when I tried using Python3 instead of cURL and was converting my JSON above into JSON to pass to the requests library. I was able to successfully get a token.
So the answer is, the above is correct, just watch your encodings!

Related

Apps script: SumUp api requests giving error 400

I am integrating online payments to a web app. To do this I am using the SumUp API. It takes simple http requests. Here is the part of the documentation I am working with: https://developer.sumup.com/docs/single-payment/
My initial request for an access token from the API works fine. But issues arise when creating a checkout resource. I have checked many times and my JSON appears to be correct. The values all appear fine too. However when I run the code the SumUp server returns me this:
Request failed for https://api.sumup.com returned code 400. Truncated server response: {"error":"Unexpected token a in JSON at position 0"}
Here is my code:
var pay_headers = {
"Authorization": `Bearer ${access_token}`,
"Content-Type": "application/json"
};
var pay_details = {
"checkout_reference": "SH8Q0B5C", //random string of letters and numbers
"amount":10,
"currency":"GBP",
"pay_to_email": "docuser#sumup.com",
"description":"Sample one-time payment"
};
var pay_options = {
"method": "post",
"headers": pay_headers,
"payload": pay_details
};
var pay_response = UrlFetchApp.fetch("https://api.sumup.com/v0.1/checkouts",pay_options).getContentText();
Is there something wrong with this? I would appreciate any help as this has been a problematic issue. Thanks
The final 403 error here is due to a poorly documented requirement on the Sumup API. Basically you have to request that your account (whether test or live) is granted scope for the payments part of the API.
If you don't have this you can get an access token but will then be served a 403 error when creating a Checkout.
Save yourself some time and follow this: (I didn't)
https://github.com/sumup/sumup-ecom-php-sdk/issues/24
i.e. email integration#sumup.com
API docs:
https://developer.sumup.com/docs/authorization/#authorization-scopes
When I saw your provided document, the sample curl is as follows. Ref
curl -X POST \
https://api.sumup.com/v0.1/checkouts \
-H 'Authorization: Bearer 565e2d19cef68203170ddadb952141326d14e03f4ccbd46daa079c26c910a864' \
-H 'Content-Type: application/json' \
-d '{
"checkout_reference": "CO746453",
"amount": 10,
"currency": "EUR",
"pay_to_email": "docuser#sumup.com",
"description": "Sample one-time payment"
}'
If this curl command is converted to Google Apps Script, how about modifying your script as follows?
From:
"payload": pay_details
To:
"payload": JSON.stringify(pay_details)
Note:
When the above modification is reflected to your script, when an error occurs, can you provide the error message?
Added:
When above modification is reflected to your script, the request is the same with the sample curl command. From your following reply,
The thing is with the previous API request made in the script (the one that fetches the access token) the payload works fine without using stringify. I did try it for this payload however and it gave me a 403 forbidden. Here is the error message: Exception: Request failed for https://api.sumup.com returned code 403. Truncated server response: {"error_message":"request_not_allowed","error_code":"FORBIDDEN","status_code":403}. This error can be replicated by removing various other parts of the request too so I'm not sure whether it is beneficial to use stringify...
If your access token and your request body are valid values, from 403 of the status code, I'm worried that in your situation, the access from Google side might not be able to be done.
Reference:
403 Forbidden

"No access to API product" error when trying to reach Data Connector API

I'm experiencing issues when trying to call the Data Connector API via Postman.
When executing the POST request to submit a data request, Postman returns the following error:
"developerMessage": "The client_id specified does not have access to the api product",
"moreInfo": "https://forge.autodesk.com/en/docs/oauth/v2/developers_guide/error_handling/",
"errorCode": "AUTH-001"
Here is what I've tried:
I have created a Forge app which has access to all APIs.
The user has Executive Overview permissions within the account (which has many projects).
I call the API using a 3-legged auth token with data:read, data:write and data:create scopes.
This is the code I'm using (exported from Postman):
curl --location --request POST 'https://developer.api.autodesk.com/data-connector/v1/accounts/<ACCOUNT_ID>/requests' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <BEARER_TOKEN>' \
--data-raw '
{
"description": "Test Extract",
"scheduleInterval": "ONE_TIME",
"effectiveFrom": "2021-01-30T12:00:00Z",
"serviceGroups": ["admin", "checklists", "dailylogs", "issues", "locations", "submittals", "cost", "rfis"]
}'
Your help would be highly appreciated. Thanks!
Jro, Simon, Data Connector API is now accessible. Could you give it a try if it can work at your side? Thank you for your patience and any feedback!
I have the exact same issue as yours, I don't know if the API is really public or not.
Hope that someone from Adesk forge team could answer us.
yes, this API is public beta now. We are working with engineer team and will get back to you soon. Our apology for the issue!

hangouts api json not working inside batch script

This command works fine if I paste into a command window directly:
curl -X POST -H "Content-type: application/json" --data "{\"text\":\"test\"}" --url "https://chat.googleapis.com/v1/spaces/[removed]/messages?key=[removed]&token=[removed]"
yet when I execute this from within a batch file, I receive the following:
{
"error": {
"code": 400,
"message": "Request contains an invalid argument.",
"status": "INVALID_ARGUMENT"
}
}
I've tried every permutation of escaping the quotes in the json data statement with no luck.
I found the issue - it's not even illustrated in the sample I posted as I had removed the URL. The issue related to a % symbol I had in the URL - in a batch script these need to be escaped by entering %% instead of %. Hope this saves someone the hours I wasted on such a stupid mistake. Thanks to those who reviewed or responded - sorry to have wasted your time :-(

Freesound API, POST Request Example Returns Error

I've been attempting to program something that interacts with Freesound.org's API, in a way similar to this example given in the Freesound API documentation...
curl -X POST -H "Authorization: Token {{your_api_key}}" -F
analysis_file=#"/path/to/your_file.json"
'https://www.freesound.org/apiv2/search/content/'
... But when I run this with my details replaced, I get this error:
{"detail":"The similarity server could not be reached or some unexpected error occurred."}
I wonder if anybody knows what might might be the cause of this? As no other info is given and I'm not experienced with restfulness etc.
It seems likely to me that the JSON is not formatted properly? But it was generated with precisely the application the documentation refers to in the example I got that code from. This is that JSON:
https://www.dropbox.com/s/kjpf9zzftsxzoar/test.json?dl=0
I'd be very grateful for any help. Cheers.
You can validate your json in online validator like jsonlint.com. And it shows the json content is a valid one.
Do you happen to know what is the error code its returning.
Here's what I'm getting from the request, looking at it in PAW:
https://imgur.com/a/AuIBR

Sending SMS using the Pushbullet API via Bash scripting (curl)

I'm working on a cli for the Pushbullet HTTP API using Bash scripting. Sending pushes (notes and links), as well as creating, deleting, and modifying contacts & devices are all straight forward using curl and Bash. However, sending SMS and files are a bit more complex, as both require sending more complex JSON-formatted requests to the server (multiple JSON-formatted requests, in the case of pushing files).
I've tried sending many variations on the following (both with and without escape characters), but the server keeps replying about JSON-formatting errors. The following code is based off of the example given in the Pushbullet HTTP API documentation.
curl -su $auth_id: -X POST https://api.pushbullet.com/v2/ephemerals --header "Content-Type: application/json"
--data-binary '{ "\"type"\": "\"push"\", "\"push"\": { \
"\"type"\": "\"messaging_extension_reply"\", \
"\"package_name"\": "\"com.pushbullet.android"\", \
"\"source_user_iden"\": "\"$source_idens"\", \
"\"target_device_iden"\": "\"$target_idens"\", \
"\"conversation_iden"\": "\"$sms_device"\", \
"\"message"\": "\"Hello"\" \
} }'
Using bash -x, I can see that this is (supposedly) what is being sent to the server:
--data-binary '{"type": "push", "push": {
"type": "messaging_extension_reply",
"package_name": "com.pushbullet.android",
"source_user_iden": "<source_idens>",
"target_device_iden": "<device_idens>",
"conversation_iden": "<sms_phone_number>",
"message": "Hello" } }'
In all cases, the server returns:
{"error":{"type":"invalid_request","message":"Failed to parse JSON body.","cat":"(=^‥^=)"}}
What is the appropriate formatting of a JSON request using curl to send an SMS via the Pushbullet API? Am I overlooking something obvious? I'm trying to accomplish this using only curl and Bash, I see no reason why it's not possible (maybe not the fastest or most elegant way, but certainly possible).
I found the solution to my issue so I thought I'd share it. It was actually very simple:
Because the curl command includes a JSON-formatted response with single quotes, variable expansion was not occurring. This is a limitation (or perhaps a feature) of Bash. So, even though the server responded with { } indicating no errors in the request, the requests were actually being sent without the proper values for parameters, such asuser_iden,source_user_iden, etc.
Solution:
Enclose all variable expansions inside the JSON-formatted request in a double-quote and single-quote, like so:
"'"$user_idens"'"
First I'd like to apologize for how bad the API is, especially file upload and sending SMS. I was thinking of adding multipart or base64 file uploads to /v2/pushes. I think the first one might help you with curl, not sure about the base64 one. multipart is a huge pain though, so I'd prefer to make it better than the current setup if possible, rather than about equally as bad. Suggestions are welcome.
I tried your command line and it seemed to work, so I'm not sure what is going wrong. Here's the command line I did. Perhaps your quote escaping or newlines are causing the JSON error?
curl -u <access_token> -X POST https://api.pushbullet.com/v2/ephemerals --header "Content-Type: application/json" --data-binary '{"type": "push", "push": {"type": "messaging_extension_reply","package_name": "com.pushbullet.android","source_user_iden": "iden","target_device_iden": "device_idens", "conversation_iden": "sms_phone_number","message": "Hello" } }'