0

Im using a code of an answer here:

Answer by BentFX

I am using wamp and I did everything as he did. The problem is that the $token value is empty. I wouldn't have asked but it is returning null and I tried almost everything. Here's the code:

index.html

<!DOCTYPE HTML>
<html>
<head>
<title>FourSquare test page...</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript">
<!--
var foursquareKey;

// Open foursquare login window.
function get4sqKey(){
  if(!foursquareKey){
    window.open('scripts/4sq_Login.php', 'foursquareAuth', 'width=960, height=548');
  }
}

// called crosswindow by login window
function set4sqKey(key){
  foursquareKey = key;
  alert(foursquareKey);
  //alert("Logged into Foursquare"); // setTimeout makes alert non-blocking

}

// simple alert to display OAuth token
function showKey(){
  alert(foursquareKey);
}

// -->
</script>
</head>
<body>
<a href="javascript:get4sqKey();">get4sqKey();</a> |
<a href="javascript:showKey();">showKey();</a>
</body>
</html>

4sq_login.php in scripts folder.

<?php

  require_once('secrets.php'); //defines CLIENT_ID

// build $url
  $url = 'https://foursquare.com/oauth2/authenticate';
  $url .= '?client_id='.CLIENT_ID;
  $url .= '&response_type=code';
  $url .= '&redirect_uri=http://localhost/scripts/4sq_Callback.php'; 

  header( 'Location: '.$url ) ;

  ?>

4sq_Callback. The directories are set and the foursquare callback link is also correct.

<?php
// Foursquare login step 2, echo back $code from QUERY_STRING
  require_once('secrets.php'); // defines CLIENT_ID & CLIENT_SECRET

// get $code from QUERY_STRING
  parse_str($_SERVER['QUERY_STRING'], $query);
  $code = $query['code'];

// build url
  $url2 = 'https://foursquare.com/oauth2/access_token';
  $url2 .= '?client_id='.CLIENT_ID;
  $url2.= '&client_secret='.CLIENT_SECRET;
  $url2 .= '&grant_type=authorization_code';
  $url2 .= '&redirect_uri=http://localhost/scripts/4sq_Callback.php'; //change to your 4sq callback
  $url2 .= "&code=.$code";

// call to https://foursquare.com/oauth2/access_token with $code
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_URL, $url2);
  $result = curl_exec($ch);
  curl_close($ch);

// $result value is json {access_token: ACCESS_TOKEN}
  $values = json_decode($result, true);
  $token = $values['access_token'];

// set access_token cookie (if you wish)

// crosswindow scripting to pass back $token
  echo('<script type="text/javascript">');
  echo('opener.set4sqKey("'.$token.'");');
  echo('self.close();'); // close self
  echo('</script>');
Community
  • 1
  • 1
Fahad Rana
  • 70
  • 1
  • 2
  • 16
  • What is the output of `var_dump($result)`? – kunruh Jun 13 '16 at 20:13
  • Also, the line `$url2 .= "&code=.$code";` looks a little suspect...Unless this is a special format requirement of the API, did you mean `$url2 .= "&code=".$code;` – kunruh Jun 13 '16 at 20:15
  • var_dump gives boolean false – Fahad Rana Jun 13 '16 at 20:16
  • No change my friend still boolean false – Fahad Rana Jun 13 '16 at 20:18
  • What about a `var_dump` on your `$code` variable? You want to make sure you are actually getting a code back. (No need to tell me what its value is, just that is what you would expect). – kunruh Jun 13 '16 at 20:26
  • a code which I then exchange for token. Everything works fine until the line $ch = curl_init(); I think the script is not calling the url correctly but I dont know why. My wamp is configured correctly for the curl method – Fahad Rana Jun 13 '16 at 20:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114662/discussion-between-fahad-rana-and-kunruh). – Fahad Rana Jun 14 '16 at 18:16

1 Answers1

0

I figured out. The problem was the cURL method was not working correctly. I simply used this line to get the access token. I dont know why cURL is not working but this method is a working substitute:

$values= file_get_contents($url2);
Fahad Rana
  • 70
  • 1
  • 2
  • 16