How can I send this curl as tcl/http instead? - api

I'm trying to trigger "play" -funtion with spotify API and tcl/http.
Here is the concept, in curl: https://developer.spotify.com/console/put-play/
curl -X "PUT" "https://api.spotify.com/v1/me/player/play" --data "{\"context_uri\":\"spotify:album:5ht7ItJgpBH7W6vJ5BqpPr\",\"offset\":{\"position\":5},\"position_ms\":0}" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer "
Here's a working example with tcl using exec curl:
set url https://api.spotify.com/v1/me/player/play?device_id=$device_id
set data "{\"context_uri\":\"spotify:album:5ht7ItJgpBH7W6vJ5BqpPr\",\"offset\":{\"position\":5},\"position_ms\":0}"
if {[catch {
exec curl -X "PUT" "$url" --data $data -H "Accept: application/json" \
-H "Content-Type: application/json" -H "Authorization: Bearer $oauth"
} msg]} then {
puts $msg
}
But i'd like to do it with tcl/http, if it's possible.
Here is my best shot so far doing this with http, but it says "malformed json".
if {$incoming == ".play"} {
::http::register https 443 {::tls::socket -autoservername 1}
::http::config -urlencoding utf-8 -useragent {JSON curl/7.55. 1}
set spot_url [http::geturl https://api.spotify.com/v1/me/player/play? \
-method PUT \
-query {{device_id:"d70b1f06*MASKED*36c308",
"context_uri":"spotify:album:5ht7ItJgpBH7W6vJ5BqpPr","offset":{"position":5},"position_ms":0}} \
-headers [list Accept application/json Content-Type application/json Authorization "Bearer $::access_token"]]
set spot_temp [::http::data $spot_url]
::http::cleanup $spot_temp
::http::cleanup $spot_url
return $spot_temp
}
Any idea how to send it correct to avoid malformed json? :D
Thanks!

Related

How to get an oauth2 url on postman

I'm trying to get the access token on Postman. I'm in "Get acces token", "body" and I'm using the 'POST' method (not the 'GET' one). When I click on the "send" button, I read this message:
{
"timestamp": "2022-11-07T21:26:28.119+00:00",
"status": 401,
"error": "Unauthorized",
"message": "",
"path": "/oidc/accessToken"
}
I think the problem is my oauth2 url. I didn't understand how to get one. I read on the internet that the url should be like this:
https://id:secret#mywebsite.com
Is it correct? I doesn't work for me.
How could I write a correct oauth2 url?
Thank you in advance!
PS: the 'code snippet' is this one:
curl --location --request POST 'https://link/accessToken' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--header 'APIM-Debug: true' \
--data-urlencode 'client_id=' \
--data-urlencode 'client_secret=' \
--data-urlencode 'username=myusername' \
--data-urlencode 'password=mypassword' \
--data-urlencode 'grant_type=client_credentials'

Revenuecat REST API - When recording a purchase got Content-Type not application/json

I'm trying to use POST methods (Revenue REST API) but I always get the error message "Content-Type not application/json".
The strange is that I'm using their website to test: [https://docs.revenuecat.com/reference#receipts][1]
API TEST (IMAGE)
curl --request POST \
--url https://api.revenuecat.com/v1/receipts \
--header 'Accept: application/json' \
--header 'Authorization: Bearer xxxx' \
--header 'Content-Type: application/json' \
--header 'X-Platform: android' \
--data '
{
"product_id": "xxxx",
"price": 12.9,
"currency": "BRL",
"is_restore": "false",
"app_user_id": "xxxx",
"fetch_token": "xxxxxx"
}
'
Any clues?
I've run the code using CURL and it works. Problem was in revenuecat website.
Their API test page has a bug. It adds an extra Content-Type: application/json
header to the request. You can see it the Metadata tab
Request Headers
Accept: application/json
Content-Type: application/json
X-Platform: stripe
Content-Type: application/json
Authorization: Bearer ********************
User-Agent: ReadMe-API-Explorer
And sends the request with an invalid header (you can check it in your browsers network traffic):
content-type: application/json, application/json

Is it possible to use curl/json with tcl and retrieve spotify API data?

I'm trying to make a simple proc in TCL that gets data from SPOTIFY API about what track I'm currently playing.
Any ideas are welcome. :D
Format:
GET https://api.spotify.com/v1/me/player/currently-playing
I need to send this:
curl -X "GET" "https://api.spotify.com/v1/me/player/currently-playing?market=ES&additional_types=episode" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer BQAai*****8n-5zXrLypj********hsgafd"
(id code for auth masked)
Reference: https://developer.spotify.com/documentation/web-api/reference/
This is a variant of Donald's solution using Tcl's built-in http support:
package require http;
package require json;
package require tls
::http::register https 443 ::tls::socket
set spotifyToken "BQAai*****8n-5zXrLypj********hsgafd"
proc spotifyGet {api args} {
global spotifyToken;
set url https://api.spotify.com/v1/$api?[http::formatQuery {*}$args]
dict set hdrs Authorization [list Bearer $spotifyToken]
dict set hdrs Accept "application/json"
set token [http::geturl $url \
-type "application/json" \
-headers $hdrs]
if {[http::status $token] eq "ok"} {
set responseBody [http::data $token]
} else {
error "Spotify call failed: [http::error $token]"
}
http::cleanup $token
return [json::json2dict $responseBody]
}
set data [spotifyGet me/player/currently-playing market ES additional_types episode]
Not so complicated, after all, but requires an additional dependency: TclTLS.
As a first cut, here's a very simple wrapper:
proc curl args {
# The options skip showing stuff you don't want in scripted form
# They also enable following redirects; you probably want to do that!
exec curl -s -S -L {*}$args
}
# Usually easiest to keep this sort of thing separate
set spotifyToken "BQAai*****8n-5zXrLypj********hsgafd"
# -G to force the use of the GET verb
set json [curl -G "https://api.spotify.com/v1/me/player/currently-playing?market=ES&additional_types=episode" -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer $spotifyToken"]
We can wrap that further:
package require http; # Standard part of Tcl
package require json; # Part of tcllib
proc spotifyGet {api args} {
global spotifyToken; # Assume variable set as earlier
set url https://api.spotify.com/v1/$api?[http::formatQuery {*}$args]
set options {-H "Accept: application/json" -H "Content-Type: application/json"}
lappend options -H "Authorization: Bearer $spotifyToken"
return [json::json2dict [curl -G $url {*}$options]]
}
set data [spotifyGet me/player/currently-playing market ES additional_types episode]
puts [dict get $data item name]
If you're doing more serious JSON work, consider using the rl_json library. And you'll need to explore the data you get back to understand it. (I can't test; I don't use Spotify.)

Curl response not decoding special chars

Im doing a http request to an Api, and my response is always not decoding special chars. Already tried to add utf-8 content type and accept enconding header but its not solving.
curl --location --request POST 'https://example.com/clients.json?api_key=12345678' --header 'Content-Type: application/json;' --header 'Cookie: _makeover_app_ix_com_session=BAh7BzoPc2Vzc2lvbl9pZCIlZTQ5YWQyYTE0YjMyZTI0OTgwNmI5NjgxZmRkYzkxZTciCmZsYXNoSUM6J0FjdGlvbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhhc2h7BjoKZXJyb3IiRXXvbWUgbsOjbyBlc3TDoSBkaXNwb27DrXZlbC48YnIvPkNvbnRyaWJ1aW50ZSBuw6NvIMOpIHbDoWxpZG88YnIvPgY6CkB1c2VkewY7B1Q%3D--9cd818580f4d9c76ac84be57dfcc33436eda021e' --data-raw '{
"client": {
"name": "Customer name"
}
}'
Response Example:
{"error":"Nome n\u00e3o est\u00e1 dispon\u00edvel."}
Those characters are UTF-8 characters, but they are also encoded using JSON / Javascript unicode escape sequences.
CURL is not going to alter the HTTP response for you, but a JSON parser will know what to do with it. If you want to do something with this on the command-line, you might like the jq command.
curl --location --request POST 'https://example.com/clients.json?api_key=12345678' --header 'Content-Type: application/json;' --header 'Cookie: _makeover_app_ix_com_session=BAh7BzoPc2Vzc2lvbl9pZCIlZTQ5YWQyYTE0YjMyZTI0OTgwNmI5NjgxZmRkYzkxZTciCmZsYXNoSUM6J0FjdGlvbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhhc2h7BjoKZXJyb3IiRXXvbWUgbsOjbyBlc3TDoSBkaXNwb27DrXZlbC48YnIvPkNvbnRyaWJ1aW50ZSBuw6NvIMOpIHbDoWxpZG88YnIvPgY6CkB1c2VkewY7B1Q%3D--9cd818580f4d9c76ac84be57dfcc33436eda021e' --data-raw '{
"client": {
"name": "Customer name"
}
}' | jq .

paypal authentication failure in sandbox

I encounter an issue when I try to use the Paypal sandbox API.
I've created my 2 sandbox accounts (the facilitator and the buyer), and I've created my app to get the credentials.
Then, I use the curl example provided by Paypal to get a token :
curl -v https://api.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "my-client-id:my-secret" \
-d "grant_type=client_credentials"
I get a 200 response, with an "access_token".
Then, I use this access token to get another resource, for example :
curl -v -X GET https://api.sandbox.paypal.com/v1/invoicing/invoices?page=3&page_size=4&total_count_required=true \
-H "Content-Type: application/json" \
-H "Authorization: Bearer the-token-received-above"
Then, I get a 401 error :
{
"name":"AUTHENTICATION_FAILURE",
"message":"Authentication failed due to invalid authentication credentials or a missing Authorization header.",
"links":[{
"href":"https://developer.paypal.com/docs/api/overview/#error",
"rel":"information_link"
}]
}
I don't understand what I'm doing wrong, since I've followed every step decribed in the Paypal doc (at least, I think I have... probably not)
Thanks for your help
curl -v -X GET "https://api.sandbox.paypal.com/v1/invoicing/invoices?page=3&page_size=4&total_count_required=true" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer the-token-received-above"
Actually had this exact same issue but didn't know what was wrong with my curl. For me, the issue was I forgot to put "Bearer" in the Authorization section.
For this, you are required to wrap the URL with quotation marks.
After get access_token. Please try this
try {
$params = array('access_token' => $jsonResponse->access_token);
$userInfo = OpenIdUserinfo::getUserinfo($params, $this->_api_context);
} catch (Exception $ex) {
ResultPrinter::printError("User Information", "User Info", null, $params, $ex);
exit(1);
}
ResultPrinter::printResult("User Information", "User Info", $userInfo->getUserId(), $params, $userInfo);
Don't forget to add
use PayPal\Api\OpenIdTokeninfo;
use PayPal\Api\OpenIdUserinfo;
That's worked for me.