0 votes

Hi all!

I am trying to get authorization in my Spotify account to retrieve some data with no success (new to Godot and programming).

According to the official example (https://developer.spotify.com/documentation/general/guides/authorization-guide/):
curl -X "POST" -H "Authorization: Basic ZjM4ZjAw...WY0MzE=" -d grant_type=client_credentials https://accounts.spotify.com/api/token

Retrieves:

{
   "access_token": "NgCXRKc...MzYjw",
   "token_type": "bearer",
   "expires_in": 3600,
}

In Godot, I got the following code (GDScript, based on official example on docs (https://docs.godotengine.org/en/latest/tutorials/networking/http_request_class.html)):

extends CanvasLayer

func _ready():
    pass # Replace with function body.

func _on_Button_pressed():
    var url : String = "https://accounts.spotify.com/api/token"
    var headers = ["Authorization: Basic ZjM4ZjAw...WY0MzE="]
    var data_fields : Dictionary = {"grant_type" : "client_credentials"}
    var http : HTTPClient = HTTPClient.new()
    var data : String = http.query_string_from_dict(data_fields)

    $HTTPRequest_POST.request(url, headers, true, HTTPClient.METHOD_POST, data)

    print(url)
    print(headers)
    print(data)


func _on_HTTPRequest_POST_request_completed(result, response_code, headers, body):
    if result == HTTPRequest.RESULT_SUCCESS:
        if response_code == 200:
            print(body.get_string_from_utf8())
        else:
            print("http Error")
            print("")
            print(result)
            print("")
            print(response_code)
            print("")
            print(headers)
            print("")
            print(body.get_string_from_utf8())

And console output is:

https://accounts.spotify.com/api/token
[Authorization: Basic ZjM4ZjAw...WY0MzE=]
grant_type=client_credentials
http Error

0

400

[date: Fri, 26 Mar 2021 22:36:15 GMT, content-type: text/html; charset=utf-8, set-cookie: __Host-device_id=AQBoU23xYfSZOVf_AhBs6ky_5JVHvFuNma2JrEIQ1vhm8Bx9wxaFP2TjIzJ4EY7Ga7wF09wMDJCevmShdOzXzjiaFqcKchGQ5Ak;Version=1;Path=/;Max-Age=2147483647;Secure;HttpOnly;SameSite=Lax, content-security-policy: default-src 'self'; script-src 'self' https://www.google-analytics.com https://ssl.google-analytics.com https://www.google.com https://www.gstatic.com/recaptcha/ https://www.google.com/recaptcha/ https://accounts.scdn.co; img-src 'self' https://i.imgur.com https://d2mv8tnci56s9d.cloudfront.net https://profile-images.scdn.co https://*.scdn.co https://graph.facebook.com https://fbcdn-profile-a.akamaihd.net https://*.fbcdn.net https://platform-lookaside.fbsbx.com https://www.google.com https://www.google-analytics.com https://stats.g.doubleclick.net data: https://accounts.scdn.co; font-src 'self' data: https://sp-bootstrap.global.ssl.fastly.net https://fonts.gstatic.com https://accounts.scdn.co; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com/css2 https://accounts.scdn.co; frame-src 'self' https://www.spotify.com https://www.google.com https://app.adjust.com https://itunes.apple.com itms-apps: https://www.google.com/recaptcha/; connect-src 'self' https://www.google-analytics.com;, x-content-security-policy: default-src 'self'; script-src 'self' https://www.google-analytics.com https://ssl.google-analytics.com https://www.google.com https://www.gstatic.com/recaptcha/ https://www.google.com/recaptcha/ https://accounts.scdn.co; img-src 'self' https://i.imgur.com https://d2mv8tnci56s9d.cloudfront.net https://profile-images.scdn.co https://*.scdn.co https://graph.facebook.com https://fbcdn-profile-a.akamaihd.net https://*.fbcdn.net https://platform-lookaside.fbsbx.com https://www.google.com https://www.google-analytics.com https://stats.g.doubleclick.net data: https://accounts.scdn.co; font-src 'self' data: https://sp-bootstrap.global.ssl.fastly.net https://fonts.gstatic.com https://accounts.scdn.co; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com/css2 https://accounts.scdn.co; frame-src 'self' https://www.spotify.com https://www.google.com https://app.adjust.com https://itunes.apple.com itms-apps: https://www.google.com/recaptcha/; connect-src 'self' https://www.google-analytics.com;, sp-trace-id: db6ec545a42cc4a5, strict-transport-security: max-age=31536000, x-content-type-options: nosniff, content-encoding: gzip, vary: Accept-Encoding, server: envoy, Via: HTTP/2 edgeproxy, 1.1 google, Alt-Svc: clear, Transfer-Encoding: chunked]

<!DOCTYPE html>
<html ng-app="accounts" ng-csp>
  <head>
    <meta charset="utf-8">
    <title>Error - Spotify</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <base href="/">
    <link rel="icon" href="https://accounts.scdn.co/oauth2/images/favicon.ace4d8543bbb017893402a1e9d1ac1fa.ico">
    <link href="https://accounts.scdn.co/oauth2/css/index.7ca2ccfe9264062ace80.css" media="screen" rel="stylesheet">
  </head>
  <body>
  <div class="head">
    <a class="spotify-logo" href="/" tabindex="-1" title="Spotify"></a>
  </div>

    <div class="container-fluid error">
      <div class="content">
        <h1 class="h1">Error</h1>
        <p>
          Oops! Something went wrong, please try again or check out our <a href="https://www.spotify.com/help">help area</a>.
        </p>
      </div>
    </div>
    <script async defer src="https://accounts.scdn.co/oauth2/js/error.6613958fda8d625b1d94.js" sp-error=''></script>
  </body>
</html>

I tested in Godot 4.0 as well as in Godot 3.2.3 with the same result.

How can I translate curl request over to a Godot script?

Godot version 4.0.dev & 3.2.3
in Engine by (12 points)
recategorized by

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.