Impossible to get an access_token for Instagram Basic Display API

前端 未结 7 2294
谎友^
谎友^ 2021-01-04 12:32

I\'m trying to get an access_token from Instagram to use their Basic Display API for a new app (simply display tweets on a webpage).

I followed these steps: https://

7条回答
  •  抹茶落季
    2021-01-04 13:15

    I am using PHP but without using any lib. Maybe this one help you.

    curl.php

    class InstagramApi 
    {
    
    public function GetAccessToken($client_id, $redirect_uri, $client_secret, $code) {      
        $url = 'https://api.instagram.com/oauth/access_token';
    
        $curlPost = 'app_id='. $client_id . '&redirect_uri=' . $redirect_uri . '&app_secret=' . $client_secret . '&code='. $code . '&grant_type=authorization_code';
        $ch = curl_init();      
        curl_setopt($ch, CURLOPT_URL, $url);        
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);      
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);            
        $data = json_decode(curl_exec($ch), true);  
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
        curl_close($ch);    
    
        if($http_code != '200')         
            throw new Exception('Error : Failed to receieve access token');
    
        return $data;
    
      }
    

    index.php

    include "curl.php";
    include "instagram_keys.php"; // holding APP ID, SECRET KEY, REDIRECT URI
    
     $instagram_ob = new InstagramApi();
     $insta_data = $instagram_ob->GetAccessToken(INSTAGRAM_CLIENT_ID, INSTAGRAM_REDIRECT_URI, INSTAGRAM_CLIENT_SECRET, $_GET['code']);  
      echo  $insta_data['access_token'];
      echo  $insta_data['user_id'];
    

    NOTE: $_GET['code'] is required and you should know how to get the code. Read here

提交回复
热议问题