简体   繁体   中英

500 Server Error When Using Foursquare API

I'm using php-foursquare library for Foursquare API calls.

This is my 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";
    }

But GetToken() method returning 500 server error. THis is source code of GetToken () method:

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;
    }

I'm not using php-foursquare, but I faced a similar problem when using Guzzle to connect to 4sq REST endpoints.

It turns out that a request will result in an unhandled exception if you don't wrap it in a try catch block and it gets anything different than a 200 header. I couldn't understand why I was getting error 500, but when I captured and printed the exception it showed Foursquare returning a 401 with a much more informative msg. In my case, the redirecting URL had a typo.

try {
$result = $this->GET($this->TokenUrl,$params);
} catch (Exception $e) {
 echo $e->getMessage();
}    

Well, here is the problem. You don't have any control over 4square's servers, so you don't have enough information to go about this without guessing. I would do two things:

  1. Google the API call you are using and see if it's a common problem with a commmon answer
  2. Email 4square - it's their server spitting out an error message with no useful information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM