Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

oauth 2.0 - Spotify oauth2 with PHP curl. How to get authorization code?

I have tried following the spotify authorization code flow, and have fallen at the first hurdle of getting user authorization.

I have tried doing this using PHP curl.

If you take a look at my code, what am I doing wrong?

$url           = 'https://accounts.spotify.com/authorize';
$redirect_uri  = urlencode( site_url() );
$curl = curl_init();
 
curl_setopt( $curl, CURLOPT_URL, $url);
curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, true );
curl_setopt( $curl, CURLOPT_HTTPGET, true );
curl_setopt( $curl, CURLOPT_POSTFIELDS, 'state=' );
curl_setopt( $curl, CURLOPT_POSTFIELDS, 'response_type=code' );
curl_setopt( $curl, CURLOPT_POSTFIELDS, 'scope=playlist-read-private%20playlist-modify-private' );
curl_setopt( $curl, CURLOPT_POSTFIELDS, 'client_id=' . $this->client_id );
curl_setopt( $curl, CURLOPT_POSTFIELDS, 'redirect_uri=' . $redirect_uri );
 
curl_exec( $curl );
curl_close( $curl );
 
$fullquery     = 'https://accounts.spotify.com/authorize?response_type=code&state=&client_id=CLIENT_ID&scope=playlist-read-private%20playlist-modify-private&redirect_uri=http%3A%2F%2Flocalhost%3A8888%2Fspotifyapi';

Running this returns a block of html from spotify, embedded into my page, with the text: "Error Oops! Something went wrong, please try again or check out our help area."

What's interesting however, is that if I put the full query into my search bar, it works exactly as I expect, by taking me to an authorization page, and after acceptance, redirecting back to my callback url, with my access code as a parameter in the url.

Why can't I get the desired effect in my PHP function?

Any hints would be greatly appreciated. I know there is a php spotify api wrapper on GitHub I could use, but I want to do this myself as a learning project.

spotify error response

question from:https://stackoverflow.com/questions/65866625/spotify-oauth2-with-php-curl-how-to-get-authorization-code

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...