[英]Handling user authorization with Instagram API
I am building a pretty simple web application. 我正在构建一个非常简单的Web应用程序。 I'm having trouble handling the code return after a user logs in with Instagram. 用户登录Instagram后,我无法处理返回的代码。 I'm also using another API to assist me you can find it here: https://github.com/cosenary/Instagram-PHP-API . 我还在使用另一个API来帮助我,您可以在这里找到它: https : //github.com/cosenary/Instagram-PHP-API 。
My homepage logs the user in you can see the php code below 我的首页记录了用户,您可以在下面看到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();
?>
Users are successfully sent to the Instagram login page but when they authorize my app and Instagram responds with a http://your-redirect-uri?code=CODE
The next page fails to render. 用户已成功发送到Instagram登录页面,但是当他们授权我的应用程序时,Instagram用http://your-redirect-uri?code=CODE
响应。下一页无法呈现。
You can see the code for the "success" page that is supposed to be displayed below. 您可以看到应该在下面显示的“成功”页面的代码。
/**
* 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'];
}
}
?>
If users cancel the authorization request the "success" page properly renders and displays the correct error message. 如果用户取消授权请求,则“成功”页面会正确呈现并显示正确的错误消息。 So I believe my issue is in the handling of the code parameter that Instagram returns to my web app. 因此,我认为我的问题在于Instagram返回我的Web应用程序的代码参数的处理。 Thanks for the help. 谢谢您的帮助。
Check that YOUR_APP_CALLBACK
redirect url is correct - it should be the same here as it is in your Instagram App. 检查您的YOUR_APP_CALLBACK
重定向URL是否正确-此处应与您的Instagram App中的URL相同。 Check out their guidelines for acceptable redirect urls here: http://instagram.com/developer/authentication/ 在此处查看他们的指南以获取可接受的重定向网址: http : //instagram.com/developer/authentication/
As long as $_GET['code'];
只要$_GET['code'];
is set then the plugin should take care of the rest so I would check that this is being set too. 设置了,那么插件应负责其余的工作,因此我将检查是否也进行了设置。
If you haven't done this already have a look at the plugin example here . 如果您尚未执行此操作,请在这里查看插件示例。 That should get you started. 那应该让您开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.