无法使用 PHP 存储 Instagram API access_token

发布于 2024-12-22 18:58:05 字数 1393 浏览 1 评论 0原文

我正在构建一个连接到 instagram API 的小型网络应用程序。目前正在使用GitHub 上的这个库,这让事情变得更容易一些。我可以正常连接并初始登录,页面将显示所有用户数据。

但是,一旦刷新页面,所有数据都会丢失,并且脚本似乎无法再找到我的访问令牌。我尝试将其存储到 PHP 会话变量中 - 但也许我错误地执行了整个过程?我只想在执行 OAuth 后在整个网站上保持相同的用户会话。

您可以在这里查看这个小应用程序:http://spyrestudios.com/demos/instagram -api/index.php

另外,我的回调 URL 是 http://spyrestudios.com/demos/instagram-api/instagammy.php - 这是在连接后立即运行的脚本。但尝试刷新页面,所有数据都消失了!另外,这里是我的一些代码,*应该使用该库来存储当前用户的访问令牌:

session_start();
require_once 'Instagram.php'; // the library code
$config = array(
    'client_id' => 'f0d225aa955c4bd9ae563f87f831efab', // Your client id
    'client_secret' => '377b77afc1274a89bd2df7d77e934689', // Your client secret
    'grant_type' => 'authorization_code',
    'redirect_uri' => 'http://spyrestudios.com/demos/instagram-api/instagammy.php', // The redirect URI you provided when signed up for the service
 );
// Instantiate the API handler object
$instagram = new Instagram($config);
$accessToken = $instagram->getAccessToken();
$_SESSION['InstagramAccessToken'] = $accessToken;

真的很难找到解决方案,所以我很感激我能得到的任何帮助。如果需要的话愿意发布我的代码示例..

提前致谢!

I'm working on building a small web app connecting into the instagram API. Currently using this library on GitHub which makes things a bit easier. I can connect and initially login fine, and the page will display all user data.

But once you refresh the page all the data is lost, and it appears the script can't find my access token anymore. I tried storing this into a PHP session variable - but maybe I'm doing the whole process incorrectly? I just want to keep the same user session throughout an entire website once the OAuth is performed.

You can check out the small app live here: http://spyrestudios.com/demos/instagram-api/index.php

Additionally my callback URL is http://spyrestudios.com/demos/instagram-api/instagammy.php - this is the script which will work right after you connect. But try refreshing the page and all the data is gone! Also here is my bit of code which *should use the library to store the current user's access token:

session_start();
require_once 'Instagram.php'; // the library code
$config = array(
    'client_id' => 'f0d225aa955c4bd9ae563f87f831efab', // Your client id
    'client_secret' => '377b77afc1274a89bd2df7d77e934689', // Your client secret
    'grant_type' => 'authorization_code',
    'redirect_uri' => 'http://spyrestudios.com/demos/instagram-api/instagammy.php', // The redirect URI you provided when signed up for the service
 );
// Instantiate the API handler object
$instagram = new Instagram($config);
$accessToken = $instagram->getAccessToken();
$_SESSION['InstagramAccessToken'] = $accessToken;

Really struggling for a solution, so I'd appreciate any help I can get. Willing to post examples of my code if needed..

Thanks in advance!

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

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

发布评论

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

评论(3

罪歌 2024-12-29 18:58:05

假设 $accessToken 实际上正确存储在会话中,您已经完成的看起来不错(尽管我没有 Instagram 经验)。

但肯定在某些时候您需要反馈 $accessToken 以便对其进行验证。就像我说的,我以前没有使用过 Instagram,所以我不知道你会如何做到这一点。

What you have already looks fine (although I have no experience in Instagram), assuming that $accessToken is actually being stored correctly in the session.

But surely at some point you need to feed back in $accessToken so that it can be verified. Like I said, I've not used Instagram before, so I don't know how you would do this.

奶气 2024-12-29 18:58:05

以防万一您仍然关心。

问题是,当您刷新或返回时,刷新会再次点击 oauth 登录页面,但没有授权它从中收集信息。理论上,您可以将会话变量的设置包装在检查周围:

$instagram = new Instagram($config);
$accessToken = $instagram->getAccessToken();
if ($accessToken != '')
    $_SESSION['InstagramAccessToken'] = $accessToken;

这将阻止不需要的调用您的 oauth 登录页面的清除当前会话值。

但更好的办法是将登陆页面重定向到不同的页面进行显示。即让您的 instagammy.php 结束并添加一个

header("Location: http://spyrestudios.com/demos/instagram-api/dosomethinghere.php");
die();

这样用户将永远不会在其历史记录中看到“instagammy.php”页面,并且无法返回到它。

Just in case you still care..

The problem is that refreshing is hitting the oauth landing page again when you refresh or back, but there is no authorization for it to collect the information from. In theory you could just wrap your setting of the session variable around a check:

$instagram = new Instagram($config);
$accessToken = $instagram->getAccessToken();
if ($accessToken != '')
    $_SESSION['InstagramAccessToken'] = $accessToken;

That would stop an unwanted to call to your oauth landing page from clearing the current session value.

But better still would be to have the landing page redirect to a different page for the display. ie have your instagammy.php end there and add a

header("Location: http://spyrestudios.com/demos/instagram-api/dosomethinghere.php");
die();

That way the user won't ever see the "instagammy.php" page in their history and won't be able to go back to it.

獨角戲 2024-12-29 18:58:05

这对我有用,而不是原始文件 instagammy.php

$instagram = new Instagram($config);
if ($_SESSION['InstagramAccessToken'] == ''){
    $accessToken = $instagram->getAccessToken();
    $_SESSION['InstagramAccessToken'] = $accessToken;
}else{
    $accessToken = $_SESSION['InstagramAccessToken'];
    $instagram->setAccessToken($_SESSION['InstagramAccessToken']);
}

This work for me, instead the original file instagammy.php

$instagram = new Instagram($config);
if ($_SESSION['InstagramAccessToken'] == ''){
    $accessToken = $instagram->getAccessToken();
    $_SESSION['InstagramAccessToken'] = $accessToken;
}else{
    $accessToken = $_SESSION['InstagramAccessToken'];
    $instagram->setAccessToken($_SESSION['InstagramAccessToken']);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文