0

我正在使用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 server error 。这是 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;
    }
4

2 回答 2

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();
}    
于 2013-07-31T22:47:35.690 回答
0

好吧,这就是问题所在。您对 4square 的服务器没有任何控制权,因此您没有足够的信息来进行此操作而无需猜测。我会做两件事:

  1. 谷歌你正在使用的 API 调用,看看它是否是一个常见问题的常见答案
  2. 向 4square 发送电子邮件 - 这是他们的服务器吐出没有有用信息的错误消息。
于 2011-07-14T22:30:27.557 回答