簡體   English   中英

使用Instagram API處理用戶授權

[英]Handling user authorization with Instagram API

我正在構建一個非常簡單的Web應用程序。 用戶登錄Instagram后,我無法處理返回的代碼。 我還在使用另一個API來幫助我,您可以在這里找到它: https : //github.com/cosenary/Instagram-PHP-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應用程序的代碼參數的處理。 謝謝您的幫助。

檢查您的YOUR_APP_CALLBACK重定向URL是否正確-此處應與您的Instagram App中的URL相同。 在此處查看他們的指南以獲取可接受的重定向網址: http//instagram.com/developer/authentication/

只要$_GET['code']; 設置了,那么插件應負責其余的工作,因此我將檢查是否也進行了設置。

如果您尚未執行此操作,請在這里查看插件示例。 那應該讓您開始。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM