2014-11-24 86 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。 這應該讓你開始。