【问题标题】:500 Server Error When Using Foursquare API使用 Foursquare API 时出现 500 服务器错误
【发布时间】:2011-07-14 22:26:00
【问题描述】:

我正在使用 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;
    }

【问题讨论】:

  • 我认为你应该做更多的调试。您能与我们分享完整的 CURL 错误和端点 URL 吗?
  • @ahmet alp balkan,请再次检查。
  • 这还不够。您应该跟踪 $this->GET 方法等以查看由foursquare 库准备的确切HTTP 查询。 4sq api 已启动并正在运行。问题可能出在您的身份验证逻辑或 php-foursquare 库本身。
  • @ahmet,我不再使用 php-foursquare。我为我的 api 调用编写了一个类,它工作得很好:)
  • 是的,正如我告诉你的那样。问题出在图书馆下面。如果该库被广泛使用,您应该报告事件。

标签: php api foursquare


【解决方案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();
}    

【讨论】:

    【解决方案2】:

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

    1. 用 Google 搜索您正在使用的 API 调用,看看这是否是常见问题的常见答案
    2. 向 4square 发送电子邮件 - 这是他们的服务器吐出没有有用信息的错误消息。

    【讨论】:

    • 4SQ 服务器状态正常。只是使用 Google Maps API 的 GeoLocate 方法。而且我最近没有使用这种方法。我已经邮寄到 4sq。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-22
    • 1970-01-01
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多