如何从带有 # 的 URL 中 $_GET['access_token']


How To $_GET['access_token'] From URL with a #

https://accounts.spotify.com/authorize?client_id=6af26783dff4466fa5cbcce0f042aba7&redirect_uri=http://bon-deals.com/School/Collegas/Heartify/callback&scope=user-read-private+user-read-email&response_type=token

单击上述链接时,Spotify API 将使用以下示例链接进行响应:

http://bon-deals.com/School/Collegas/Heartify/callback#access_token=BQALr_8ac05PFDIo43oPddsSJafQNXn2nEgqR0FJALJov8dJktgrSzRoAYCGijlXyLVOZYHryhvj7TXsTh6wc3ntVrOMx36HscqS_pA9b6978bMjIP9U6IWSu-aErTRwc2rScM4Y7iJtpKRsid0MGMCLq3tPvCQ&token_type=Bearer&expires_in=3600

由于 URL 中access_token之前的#,我无法$_GET['access_token']

我们还尝试通过更改response_type=code来获取访问令牌,然后使用 cURL POST 方法将代码传输到访问令牌,但也没有工作。

您无法访问所谓的 URL 服务器端片段,因为片段部分仅适用于用户代理(即浏览器(。请求的响应类型token指示调用方希望在片段中传递令牌,因为它是浏览器内客户端或本机应用程序。

如果您希望将令牌传递到服务器端后端,则需要使用其他类型的响应,例如 response_type=code .然后,您可以通过调用 Spotify 的令牌端点将code交换为access_token

如果这对您不起作用,那将是另一个问题,包括错误描述/日志等。

你做对了吗?

你得到了什么回应?

您是否检查了 HTTP 响应标头?

当 Z @hans说"因为片段部分仅适用于用户代理(即浏览器("时,这是不正确的。

问题通常出在请求标头中。 下面的 curl 代码将获取任何用户代理可以获得的每个细节。 您只需要正确设置请求标头。 您可能会删除 Cookie:

如果您需要发帖,请添加:

$post = 'key1=value1&key2=value2&key3=value3';
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

.PHP

$request = array();
$request[] = 'Host: xxxxxxx';
$request[] = 'User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:39.0) Gecko/20100101 Firefox/39.0';
$request[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$request[] = 'Accept-Language: en-US,en;q=0.5';
$request[] = 'Accept-Encoding: gzip, deflate';
$request[] = 'DNT: 1';
$request[] = 'Cookie: xxxx
$request[] = 'Connection: keep-alive';
$request[] = 'Pragma: no-cache';
$request[] = 'Cache-Control: no-cache';
$url = 'https://xxxx.com/';
 $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_ENCODING,"");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HEADER, true);
  curl_setopt($ch, CURLINFO_HEADER_OUT, true);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_FILETIME, true);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100);
  curl_setopt($ch, CURLOPT_VERBOSE, true);
  curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  curl_setopt($ch, CURLOPT_TIMEOUT,100);
  curl_setopt($ch, CURLOPT_FAILONERROR,true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
  $data = curl_exec($ch);
  if (curl_errno($ch)){
      $data .= 'Retreive Base Page Error: ' . curl_error($ch);
  }
  else {
    $skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE)); 
    $responseHeader = substr($data,0,$skip);
    $data= substr($data,$skip);
    $info = curl_getinfo($ch);
    $info = var_export($info,true);
   }
   echo $responseHeader . $info . $data;