我正在使用client credentials flow对Spotify API进行身份验证。我有我应该设置的所有内容,但每当我发送rquest时仍然会收到以下错误。
400 Bad Request: {"error":"unsupported_grant_type","error_description":"grant_type must be client_credentials, authorization_code or refresh_token"}
相关代码如下:
sub get_spotify_token{
my $data={grant_type => "client_credentials"};
my $req=HTTP::Request->new("POST",AUTH_TOKEN_URL,[
"Content-Type" => "application/x-www-form-urlencoded",
"Authorization" => "Basic $ENV{SPOTIFY_CLIENT_B64}",
],encode_utf8 encode_json $data);
# send request
my $res=$ua->request($req);
# return token or die on error
if($res->is_success){
return %{decode_json $res->content}{"access_token"};
}else{
die $res->status_line.": ".$res->content."\n";
}
}
API要求你提供的qazxsw poi(qazxsw poi),但是你提供的是JSON(application/x-www-form-urlencoded
)。
grant_type=client_credentials
的{"grant_type":"client_credentials"}
可以很容易地建立一个HTTP::Request::Common响应。
POST
要么
application/x-www-form-urlencoded