<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Unable to use PKCE authorization: code_verifier was incorrect in Spotify for Developers</title>
    <link>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-use-PKCE-authorization-code-verifier-was-incorrect/m-p/5006416#M811</link>
    <description>&lt;P&gt;I'm trying to implement the PKCE authorization flow and as far as I can tell, I've done so correctly, but Spotify always says the code_verifier is incorrect.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my code to create the login URL:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;    this.verifyCode = getRandomString(64)

    const codeChallenge = crypto
      .createHash('sha256')
      .update(this.verifyCode)
      .digest('base64')

    const params = new URLSearchParams()

    params.append('client_id', process.env.VUE_APP_SPOTIFY_CLIENT_ID as string)
    params.append('response_type', 'code')
    params.append('scope', SPOTIFY_SCOPES.join(' '))
    params.append('redirect_uri', SPOTIFY_REDIRECT_URI)
    params.append('code_challenge_method', 'S256')
    params.append('code_challenge', codeChallenge)

    const uri: string = `https://accounts.spotify.com/authorize?${params.toString()}`&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;And to fetch the token:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;    const code = new URLSearchParams(new URL(redirectUri).search).get('code') as string

    const params = new URLSearchParams()

    params.append('client_id', process.env.VUE_APP_SPOTIFY_CLIENT_ID as string)
    params.append('grant_type', 'authorization_code')
    params.append('code', code)
    params.append('redirect_uri', SPOTIFY_REDIRECT_URI)
    params.append('code_verifier', this.verifyCode)

    const { data } = await Axios.post('https://accounts.spotify.com/api/token', params)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But Spotify always returns this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;    data: {
      error: 'invalid_grant',
      error_description: 'code_verifier was incorrect'
    }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried doing the code challenge encoding differently (base64url encoding) but it seems to have no effect.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas?&lt;/P&gt;</description>
    <pubDate>Tue, 28 Jul 2020 08:15:52 GMT</pubDate>
    <dc:creator>Christilut</dc:creator>
    <dc:date>2020-07-28T08:15:52Z</dc:date>
    <item>
      <title>Unable to use PKCE authorization: code_verifier was incorrect</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-use-PKCE-authorization-code-verifier-was-incorrect/m-p/5006416#M811</link>
      <description>&lt;P&gt;I'm trying to implement the PKCE authorization flow and as far as I can tell, I've done so correctly, but Spotify always says the code_verifier is incorrect.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my code to create the login URL:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;    this.verifyCode = getRandomString(64)

    const codeChallenge = crypto
      .createHash('sha256')
      .update(this.verifyCode)
      .digest('base64')

    const params = new URLSearchParams()

    params.append('client_id', process.env.VUE_APP_SPOTIFY_CLIENT_ID as string)
    params.append('response_type', 'code')
    params.append('scope', SPOTIFY_SCOPES.join(' '))
    params.append('redirect_uri', SPOTIFY_REDIRECT_URI)
    params.append('code_challenge_method', 'S256')
    params.append('code_challenge', codeChallenge)

    const uri: string = `https://accounts.spotify.com/authorize?${params.toString()}`&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;And to fetch the token:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;    const code = new URLSearchParams(new URL(redirectUri).search).get('code') as string

    const params = new URLSearchParams()

    params.append('client_id', process.env.VUE_APP_SPOTIFY_CLIENT_ID as string)
    params.append('grant_type', 'authorization_code')
    params.append('code', code)
    params.append('redirect_uri', SPOTIFY_REDIRECT_URI)
    params.append('code_verifier', this.verifyCode)

    const { data } = await Axios.post('https://accounts.spotify.com/api/token', params)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But Spotify always returns this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;    data: {
      error: 'invalid_grant',
      error_description: 'code_verifier was incorrect'
    }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried doing the code challenge encoding differently (base64url encoding) but it seems to have no effect.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas?&lt;/P&gt;</description>
      <pubDate>Tue, 28 Jul 2020 08:15:52 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-use-PKCE-authorization-code-verifier-was-incorrect/m-p/5006416#M811</guid>
      <dc:creator>Christilut</dc:creator>
      <dc:date>2020-07-28T08:15:52Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to use PKCE authorization: code_verifier was incorrect</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-use-PKCE-authorization-code-verifier-was-incorrect/m-p/5007599#M841</link>
      <description>&lt;P&gt;I had the exact same problem, which was resolved by radically changing how I was generating the PKCE challenge. This set of functions worked for me:&amp;nbsp;&lt;A href="https://stackoverflow.com/a/59913241" target="_blank"&gt;https://stackoverflow.com/a/59913241&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jul 2020 22:16:40 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-use-PKCE-authorization-code-verifier-was-incorrect/m-p/5007599#M841</guid>
      <dc:creator>chanderson0</dc:creator>
      <dc:date>2020-07-29T22:16:40Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to use PKCE authorization: code_verifier was incorrect</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-use-PKCE-authorization-code-verifier-was-incorrect/m-p/5008061#M847</link>
      <description>&lt;P&gt;Thanks! I tried it that way and it worked.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For anyone running into this, I changed the encoding to the following:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;async function sha256(plain) {
  const encoder = new TextEncoder()
  const data = encoder.encode(plain)

  return window.crypto.subtle.digest('SHA-256', data)
}

function base64urlencode(a){
  return btoa(String.fromCharCode.apply(null, new Uint8Array(a))
    .replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')
}

const hashed = await sha256(verifyCode)
const codeChallenge = base64urlencode(hashed)&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 30 Jul 2020 14:04:01 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-use-PKCE-authorization-code-verifier-was-incorrect/m-p/5008061#M847</guid>
      <dc:creator>Christilut</dc:creator>
      <dc:date>2020-07-30T14:04:01Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to use PKCE authorization: code_verifier was incorrect</title>
      <link>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-use-PKCE-authorization-code-verifier-was-incorrect/m-p/5045990#M1262</link>
      <description>&lt;P&gt;So can you confirm that the code challenge does not need to be padded with the `=` character?&lt;/P&gt;</description>
      <pubDate>Fri, 02 Oct 2020 23:51:48 GMT</pubDate>
      <guid>https://community.spotify.com/t5/Spotify-for-Developers/Unable-to-use-PKCE-authorization-code-verifier-was-incorrect/m-p/5045990#M1262</guid>
      <dc:creator>Peter_Schorn</dc:creator>
      <dc:date>2020-10-02T23:51:48Z</dc:date>
    </item>
  </channel>
</rss>

