Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.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/1/dart/3.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
Flutter 飞镖/颤振中的Spotify PKCE:“;代码“验证器不正确”;_Flutter_Dart_Oauth_Spotify_Pkce - Fatal编程技术网

Flutter 飞镖/颤振中的Spotify PKCE:“;代码“验证器不正确”;

Flutter 飞镖/颤振中的Spotify PKCE:“;代码“验证器不正确”;,flutter,dart,oauth,spotify,pkce,Flutter,Dart,Oauth,Spotify,Pkce,通过Spotify API的PKCE使用授权代码流,我得到一个错误,即我的代码验证器不正确,据我所知,这一定是一个编码问题。 {“error”:“invalid\u grant”,“error\u description”:“code\u verifier is error”} 这是我写的原始代码: String getAuthUrl() { code = getRandomString(128); // saveToPrefs("verfifier_code"

通过Spotify API的PKCE使用授权代码流,我得到一个错误,即我的代码验证器不正确,据我所知,这一定是一个编码问题。
{“error”:“invalid\u grant”,“error\u description”:“code\u verifier is error”}

这是我写的原始代码:

String getAuthUrl() {
    code = getRandomString(128);
    // saveToPrefs("verfifier_code", code);
    var hash = sha256.convert(ascii.encode(code));
    String code_challenge = base64Url.encode(hash.bytes);
    return Uri.parse(
            "https://accounts.spotify.com/authorize?response_type=code&client_id=${widget.client_id}&redirect_uri=http%3A%2F%2Flocalhost%2Fauth&scope=user-top-read&code_challenge=$code_challenge&code_challenge_method=S256")
        .toString();
  }

这就是我对Spotify授权指南()的理解

找到本文()后,我尝试将修复程序移植到Dart,但失败了。据我所知,该代码对ascii编码的代码进行散列,然后使用btoa()再次将其转换为ascii。这个函数似乎也可以执行base64(但不是base64Url,也许这就是为什么某些部分必须手动更换的原因?)

我还尝试了不同的编码方式:
-使用String.codeUnits(但这是使用UTF-16)
-获取sha256函数的字符串以及带有String.fromCharCodes()的base64(-Url)-函数(应该使用ASCII?)
-在ASCII和UTF-8之间切换(在这种情况下,这不会产生任何区别,因为我的验证码仅由ASCII字符组成)

编辑:
要提出请求,我使用:

var res = await http.post(endpoint, body: {"client_id":widget.client_id, "grant_type":"authorization_code", "code":value, "redirect_uri":"http://localhost/auth", "code_verifier":code});

经过进一步的研究,我发现最重要的事情是必须删除挑战结束时的“=”(base64Url不应该这样做吗?)。无论如何,这就是工作代码:

编辑代码:


编辑:

此外,还必须将“+”替换为“-”,并将“/”替换为“\u3”

我花了很多时间想清楚这一点,但没有成功。这立刻解决了问题。非常感谢。
var res = await http.post(endpoint, body: {"client_id":widget.client_id, "grant_type":"authorization_code", "code":value, "redirect_uri":"http://localhost/auth", "code_verifier":code});
String getAuthUrl() {
    code = getRandomString(128);
    var hash = sha256.convert(ascii.encode(code));
    String code_challenge = base64Url.encode(hash.bytes).replaceAll("=", "").replaceAll("+", "-").replaceAll("/", "_");
    return Uri.parse(
            "https://accounts.spotify.com/authorize?response_type=code&client_id=${widget.client_id}&redirect_uri=http%3A%2F%2Flocalhost%2Fauth&scope=user-top-read&code_challenge=$code_challenge&code_challenge_method=S256")
        .toString();
  }