Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl Spotify API不支持的授权类型_Perl_Http_Oauth_Spotify - Fatal编程技术网

Perl Spotify API不支持的授权类型

Perl Spotify API不支持的授权类型,perl,http,oauth,spotify,Perl,Http,Oauth,Spotify,我正在使用对Spotify API进行身份验证。我已经把我需要的一切都设置好了,但是每当我发送请求时,仍然会出现以下错误 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

我正在使用对Spotify API进行身份验证。我已经把我需要的一切都设置好了,但是每当我发送请求时,仍然会出现以下错误

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需要您声称提供的
application/x-www-form-urlencoded
grant\u type=client\u credentials
),但您提供的是JSON(
{“grant\u type”:“client\u credentials”}

POST
使构建
应用程序/x-www-form-urlencoded
响应变得简单

use HTTP::Request::Common qw( POST );

my $req = POST(AUTH_TOKEN_URL,
    [
        grant_type => "client_credentials",
    ],
    Authorization => "Basic $ENV{SPOTIFY_CLIENT_B64}",
    Content_Type  => 'application/x-www-form-urlencoded',
);


API需要您声称提供的
application/x-www-form-urlencoded
grant\u type=client\u credentials
),但您提供的是JSON(
{“grant\u type”:“client\u credentials”}

POST
使构建
应用程序/x-www-form-urlencoded
响应变得简单

use HTTP::Request::Common qw( POST );

my $req = POST(AUTH_TOKEN_URL,
    [
        grant_type => "client_credentials",
    ],
    Authorization => "Basic $ENV{SPOTIFY_CLIENT_B64}",
    Content_Type  => 'application/x-www-form-urlencoded',
);


1)
encode\u json
已经使用UTF-8编码了,所以
encode\u utf8
不应该在那里。2)您声称内容是
application/x-www-form-urlencoded
,但您提供了json。API实际期望的是什么?@ikegami我试图省略
encode\u utf8
,得到了相同的结果。API需要
application/x-www-form-urlencoded
,但是如果我尝试将普通哈希传递给
HTTP::Request::new
,我会收到一个错误,抱怨它是非标量引用。1)
encode\u json
已经使用UTF-8编码了,所以
encode\u utf8
不应该在那里。2)您声称内容是
application/x-www-form-urlencoded
,但您提供了JSON。API实际期望的是什么?@ikegami我试图省略
encode\u utf8
,得到了相同的结果。API需要
application/x-www-form-urlencoded
,但是如果我尝试将普通哈希传递给
HTTP::Request::new
,我会收到一个错误,抱怨它是非标量引用。我不知道我可以将
内容作为头传递,谢谢。标记为已接受。修复了一个错误。//我更喜欢使用
Content
,因为它允许我将数据“按顺序”放置,但是有一个替代调用约定,它不会使它看起来像一个标题。我已将其添加到答案中,以便您可以选择您喜欢的答案。我不知道我可以将
内容作为标题传递,谢谢。标记为已接受。修复了一个错误。//我更喜欢使用
Content
,因为它允许我将数据“按顺序”放置,但是有一个替代调用约定,它不会使它看起来像一个标题。我已经把它添加到了答案中,所以你可以选择你喜欢的。