简体   繁体   English

Spotify oauth 在尝试获取 access_token 时返回 400

[英]Spotify oauth is returning a 400 when trying to get access_token

I am trying to get an access_token from the spotify api.我正在尝试从 spotify api 获取 access_token。 spotify docs 发现文档

However i am always getting a response 400, {"error"=>"invalid_grant", "error_description"=>"Invalid authorization code"}但是我总是收到响应 400, {"error"=>"invalid_grant", "error_description"=>"Invalid authorization code"}

The code i am running was originally, the authorization_code is the from the controller and it is what is returned in the params[:code]我正在运行的代码最初是,authorization_code 来自 controller,它是params[:code]中返回的内容

    response = HTTParty.post(
      'https://accounts.spotify.com/api/token',
      body: {
         grant_type: 'authorization_code',
         code: authorization_code,
         redirect_uri: redirect_uri
        }
      ),
      headers: {
        'Authorization' => 'Basic ' + Base64.strict_encode64("#{client_id}:#{client_secret}"),
      }
    )

I read about form encoding so i tried我阅读了有关表单编码的信息,所以我尝试了

    response = HTTParty.post(
      'https://accounts.spotify.com/api/token',
      body: URI.encode_www_form(
        {
         grant_type: 'authorization_code',
         code: authorization_code,
         redirect_uri: redirect_uri
        }
      ),
      headers: {
        'Authorization' => 'Basic ' + Base64.strict_encode64("#{client_id}:#{client_secret}"),
        'Content-Type' => 'application/x-www-form-urlencoded'
      }
    )

but this has been to no avail... i have tried what i can think of as every combination and cannot get it to work.但这无济于事......我已经尝试了我能想到的每一种组合,但无法让它发挥作用。

The redirect_uri is definitely right as when i change that it gives a bad_uri method and the authorization works as i can query the api with Implicit Grant redirect_uri 绝对正确,因为当我更改它提供 bad_uri 方法并且授权有效时,我可以使用Implicit Grant查询 api

If anyone has any experience/solutions i would be much obliged如果有人有任何经验/解决方案,我将不胜感激

I think you don't need to encode the body on your own.我认为你不需要自己编码身体。 Setting the header should be enough.设置 header 应该足够了。 You may have to URL encode the redirect_uri , as it must match with the value used in the first/authorization code request.您可能必须对redirect_uri进行 URL 编码,因为它必须与第一个/授权代码请求中使用的值匹配。

response = HTTParty.post('https://accounts.spotify.com/api/token',
  body: {
    grant_type: 'authorization_code',
    code: authorization_code,
    redirect_uri: redirect_uri
  },
  headers: {
    'Authorization' => 'Basic ' + Base64.strict_encode64("#{client_id}:#{client_secret}"),
    'Content-Type' => 'application/x-www-form-urlencoded'
  }
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试从Google OAuth 2.0获取access_token时为什么会出现404错误? - Why do I get a 404 Error when attempting to get an access_token from google OAuth 2.0? 获取ruby中的oauth2 access_token所有者电子邮件地址? - Get oauth2 access_token owner email address in ruby? 无法使用Rails和OAuth gem获得我的access_token - Can't get my access_token using Rails and the OAuth gem 如何在删除用户帐户时重置omniauth-google-oauth2 access_token - How to reset omniauth-google-oauth2 access_token when user account has been removed SessionsController#create中的NoMethodError-#的未定义方法&#39;[]&#39; <Oauth::Access_Token…> - NoMethodError in SessionsController#create - Undefined method '[]' for #<Oauth::Access_Token…> Ruby on Rails-SoundCloud OAuth 2未定义方法`access_token&#39; - Ruby on Rails - SoundCloud OAuth 2 undefined method `access_token' 尝试获取身份验证令牌时出现400错误-Twitter omniauth - Getting 400 error when trying to get auth token - twitter omniauth 保存 access_token - Save access_token 如果我已经具有access_token,如何获取omniauth哈希? - How to get omniauth hash if I already have the access_token? 来自具有永久access_token的Rails应用的GET请求 - GET request from rails app with permanent access_token
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM