2014-11-24 165 views
0

我正在构建一个非常简单的Web应用程序。在用户使用Instagram登录后,我无法处理代码返回。我还使用另一个API来帮助我,你可以在这里找到它:https://github.com/cosenary/Instagram-PHP-API使用Instagram处理用户授权API

我的主页上登录你的用户可以看到下面的PHP代码

<?php 

require 'Instagram.php'; 
use MetzWeb\Instagram\Instagram; 

// initialize class 
    $instagram = new Instagram(array(
    'apiKey'  => 'YOUR_APP_KEY', 
    'apiSecret' => 'YOUR_APP_SECRET', 
    'apiCallback' => 'YOUR_APP_CALLBACK' // must point to success.php 
    )); 

// create login URL 
$loginUrl = $instagram->getLoginUrl(); 

?> 

用户被成功发送到Instagram的登录页面,但是当他们授权应用和Instagram与http://your-redirect-uri?code=CODE响应下一个页面无法渲染。

您可以看到下面应该显示的“成功”页面的代码。

/** 
* Instagram PHP API 
* 
* @link https://github.com/cosenary/Instagram-PHP-API 
* @author Christian Metz 
* @since 01.10.2013 
*/ 

require_once 'Instagram.php'; 
use MetzWeb\Instagram\Instagram; 

// initialize class 
$instagram = new Instagram(array(
'apiKey'  => 'YOUR_APP_KEY', 
'apiSecret' => 'YOUR_APP_SECRET', 
'apiCallback' => 'YOUR_APP_CALLBACK' // must point to success.php 
)); 

// receive OAuth code parameter 
$code = $_GET['code']; 

// check whether the user has granted access 
if (isset($code)) { 

// receive OAuth token object 
$data = $instagram->getOAuthToken($code); 
$username = $username = $data->user->username; 

// store user access token 
$instagram->setAccessToken($data); 

// now you have access to all authenticated user methods 
$result = $instagram->getUserMedia(); 

} else { 

// check whether an error occurred 
if (isset($_GET['error'])) { 
echo 'An error occurred: ' . $_GET['error_description']; 
} 

} 

?> 

如果用户取消授权请求,“成功”页面会正确呈现并显示正确的错误消息。所以我相信我的问题在于处理Instagram返回到我的web应用程序的代码参数。谢谢您的帮助。

回答

0

检查YOUR_APP_CALLBACK重定向网址是否正确 - 它应该与您在Instagram应用中的相同。看看他们的可接受的重定向网址指南:http://instagram.com/developer/authentication/

只要设置了$_GET['code'];那么插件应该照顾其余的,所以我会检查是否也设置了这个。

如果你还没有这样做,已经看看插件示例here。 这应该让你开始。