Foursquare api返回null access_token

时间:2016-06-13 20:07:19

标签: php foursquare

我在这里使用答案的代码:

Answer by BentFX

我正在使用wamp,我像他一样做了一切。问题是$ token值为空。我不会问,但它返回null,我几乎尝试了一切。  这是代码:

的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

<?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。目录已设置且foursquare回调链接也正确。

<?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>');

1 个答案:

答案 0 :(得分:0)

我发现了。问题是cURL方法无法正常工作。 我只是用这一行来获取访问令牌。我不知道为什么cURL不起作用,但这种方法是一种可行的替代方法:

var edit = outDocument.DocumentNode.SelectSingleNode("//textarea[@id='wpTextbox1']//text()");
String _edit;
_edit = edit.InnerText.ToString().Trim();