douzhuo6931 2015-10-23 12:32
浏览 42
已采纳

如何使用instafeed.js登录用户图像

I'm building an application where users can click a button to show all of their Instagram images on a page, but in order to do that I need userId and accessToken. How do I get these?

NOTE: If it makes any difference: I'm not trying to get just my own images, but anyone who uses my app to log into their account.

I have this code:

<script>
var feed = new Instafeed ({
    get: "user",
    userId: ,
    accessToken: ""
});
feed.run();
</script>

<div id="instafeed"></div>

And I get a verification code by clicking on a link (note I removed cliend_id and redirect_uri):

    <a href="https://api.instagram.com/oauth/authorize/?client_id=CLIENT_ID&redirect_uri=REDIRECT_URI&response_type=code">Get Instagram images</a>

Then this returns a code like '1251251...' which I grab with GET, but after that what do I do? How do I get userId and accesToken from this?

I'm not sure if I'm on the right track, but any help would be appreciated!

  • 写回答

1条回答 默认 最新

  • dragonlew9876 2015-10-23 14:59
    关注

    Answering my own question, as I figured it out:

    $url = 'https://api.instagram.com/oauth/access_token';
    
    $data = array(
        'client_id' => $clientId,
        'client_secret' => $clientSecret,
        'grant_type' => 'authorization_code',
        'redirect_uri' => $redirectUri,
        'code' => $code
    );
    
    // use key 'http' even if you send the request to https://...
    $options = array(
        'http' => array(
            'header'  => "Content-type: application/x-www-form-urlencoded
    ",
            'method'  => 'POST',
            'content' => http_build_query($data),
        ),
    );
    
    $context  = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    

    $clientId, $clientSecret and $redirectUri are taken from: https://instagram.com/developer/clients/manage/

    $code is the code from your url and $result contains the userId and accessToken if successful.

    Thanks to: How do I send a POST request with PHP?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么