使用 PHP 与 FourSquare API V2 连接

发布于 2024-11-10 08:36:17 字数 440 浏览 6 评论 0原文

我正在使用 本指南用于获得快速而肮脏的 OAuth 身份验证,以便我可以使用 API。不幸的是,他的教程似乎参差不齐。在他的链接中找不到引用的 EpiOAuth.php 文件,我必须进行谷歌搜索才能找到副本。第一次运行index.php来获取我的令牌时,我在index.php中的$results = $foursquareObj->getAuthorizeUrl()行上收到了“缺少参数”错误。即使通过手动放置我的回调 URL,我的令牌也会返回为“h”:

Login Via Foursquare
string(1) "h" 

这显然是不对的。我(他)怎么做错了?

I'm using this guide to get a quick and dirty OAuth authentication so I can play around with the API. Unfortunately his tutorial seems spotty. The EpiOAuth.php file referenced wasn't found in his link and I had to do a google search to find a copy. Upon running index.php for the first time to get my token I received a 'missing parameter' error on the $results = $foursquareObj->getAuthorizeUrl() line in index.php. Even by manually placing my callback URL in, my token comes back as "h":

Login Via Foursquare
string(1) "h" 

Which, obviously, isn't right. How am I (he) doing this wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

胡渣熟男 2024-11-17 08:36:17

这不是对您问题的直接回答,但对于获取 OAuth 令牌来说非常简单。我刚刚开始使用 foursquare,这就是我所拥有的一切。它不会执行任何类型的错误检查,但它会获取一个 OAuth 令牌,允许您查看 API。

localhost/scripts/secrets.php

<?php  
  // insert your foursquare API keys
  define('CLIENT_ID', 'YOUR_CLIENT_ID');
  define('CLIENT_SECRET', 'YOUR_CLIENT_SECRET');

localhost/scripts/4sq_Login.php

<?php
// Foursquare login stage 1, build url and redirect
  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'; // change to your 4sq callback

// redirect
  header( 'Location: '.$url ) ;

localhost/scripts/4sq_Callback.php

<?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
  $url = 'https://foursquare.com/oauth2/access_token';
  $url .= '?client_id='.CLIENT_ID;
  $url .= '&client_secret='.CLIENT_SECRET;
  $url .= '&grant_type=authorization_code';
  $url .= '&redirect_uri=http://localhost/scripts/4sq_Callback.php'; //change to your 4sq callback
  $url .= '&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, $url);
  $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)
  $expire = time()+2592000; // 30 days from now
  setcookie("foursquare_token", $token, $expire, '/');

// crosswindow scripting to pass back $token
  echo('<script type="text/javascript">');
  echo('opener.set4sqKey("'.$token.'");');
  echo('self.close();'); // close self
  echo('</script>');

localhost/index.htm

<!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;
  setTimeout('alert("Logged into Foursquare");', 1); // 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>

我不擅长解释,所以我希望你能看到我在做什么,并可以在必要时在此基础上进行构建。

(由于 PHP 处理本地文件系统,因此实际上最好将 Secrets.php 重新定位到 Web 服务器路径之外的位置。以防万一:)

This isn't a direct answer to your question, but is pretty straight forward for getting an OAuth token. I'm just getting started with foursquare, and this is about all I've got. It does not do any kind of error checking, but it does get an OAuth token which allows you to poke at the API.

localhost/scripts/secrets.php

<?php  
  // insert your foursquare API keys
  define('CLIENT_ID', 'YOUR_CLIENT_ID');
  define('CLIENT_SECRET', 'YOUR_CLIENT_SECRET');

localhost/scripts/4sq_Login.php

<?php
// Foursquare login stage 1, build url and redirect
  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'; // change to your 4sq callback

// redirect
  header( 'Location: '.$url ) ;

localhost/scripts/4sq_Callback.php

<?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
  $url = 'https://foursquare.com/oauth2/access_token';
  $url .= '?client_id='.CLIENT_ID;
  $url .= '&client_secret='.CLIENT_SECRET;
  $url .= '&grant_type=authorization_code';
  $url .= '&redirect_uri=http://localhost/scripts/4sq_Callback.php'; //change to your 4sq callback
  $url .= '&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, $url);
  $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)
  $expire = time()+2592000; // 30 days from now
  setcookie("foursquare_token", $token, $expire, '/');

// crosswindow scripting to pass back $token
  echo('<script type="text/javascript">');
  echo('opener.set4sqKey("'.$token.'");');
  echo('self.close();'); // close self
  echo('</script>');

localhost/index.htm

<!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;
  setTimeout('alert("Logged into Foursquare");', 1); // 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>

I'm not good at explaining, so I hope you can see what I'm doing, and can build on it as necessary.

(Since PHP deals with the local file system it is actually preferable to relocate your secrets.php to a location outside of the web server path. Just in case :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文