我正在使用php-foursquare库进行Foursquare API调用。
这是我的index.php
require_once("FoursquareAPI.class.php");
$client_key = "blabla";
$client_secret = "blabla";
$redirect_uri = "http://localhost/4kare/index.php";
// ($redirected_uri equals to registered callback url)
// Load the Foursquare API library
$foursquare = new FoursquareAPI($client_key,$client_secret);
// If the link has been clicked, and we have a supplied code, use it to request a token
if(array_key_exists("code",$_GET)){
// example $_GET['code'] = FJRW1Z5TZ3H0E3Y2WN4Q0UPSH1PEIDADTZDHYKVG32DJTH2E
$token = $foursquare->GetToken($_GET['code'],$redirect_uri);
}
// If we have not received a token, display the link for Foursquare webauth
if(!isset($token)){
echo "<a href='".$foursquare->AuthenticationLink($redirect_uri)."'>Connect to this app via Foursquare</a>";
// Otherwise display the token
}else{
echo "Your auth token: $token";
}
但GetToken()方法返回500服务器错误。这是GetToken()方法的源代码:
public function GetToken($code,$redirect){
$params = array("client_id"=>$this->ClientID,
"client_secret"=>$this->ClientSecret,
"grant_type"=>"authorization_code",
"redirect_uri"=>$redirect,
"code"=>$code);
$result = $this->GET($this->TokenUrl,$params);
$json = json_decode($result);
$this->SetAccessToken($json->access_token);
return $json->access_token;
}
答案 0 :(得分:1)
我没有使用php-foursquare,但在使用Guzzle连接到4sq REST端点时遇到了类似的问题。
事实证明,如果你没有将它包装在try catch块中,并且它得到的东西与200头不同,那么请求将导致未处理的异常。我无法理解为什么我得到错误500,但是当我捕获并打印异常时,它显示Foursquare返回401并提供更多信息的消息。就我而言,重定向URL有一个错字。
try {
$result = $this->GET($this->TokenUrl,$params);
} catch (Exception $e) {
echo $e->getMessage();
}
答案 1 :(得分:0)
嗯,这是问题所在。您无法控制4square的服务器,因此您没有足够的信息可以在不猜测的情况下进行此操作。我会做两件事: