Impossible to get an access_token for Instagram Basic Display API

前端 未结 7 2208
谎友^
谎友^ 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 12:59

    I was using the old Instagram API as well. I had to change a few things to make my code work on the new API. Not sure what you're using, this is how I did it with PHP.

    $url = 'https://api.instagram.com/oauth/access_token';
    
    $fields = array(
        'app_id' => 'YOUR_APP_ID',
        'app_secret' => 'YOUR_APP_SECRET_ID',
        'grant_type' => 'authorization_code',
        'redirect_uri' => 'YOUR_REDIRECT_URL',
        'code' => $code
    );
    
    $ch = curl_init();
    
    curl_setopt($ch,CURLOPT_URL, $url);
    curl_setopt($ch,CURLOPT_POST, true);
    curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch,CURLOPT_VERIFYPEER, false);
    
    $result = curl_exec($ch);
    curl_close($ch);
    
    //get the access token from the string sent from Instagram
    $splitString = explode('"access_token":', $result);
    $removeRest = explode(',', $splitString[1]);
    $withSpace = str_replace('"','', $removeRest[0]);
    $access_token = str_replace(' ','', $withSpace);
    
    0 讨论(0)
  • 2021-01-04 13:02

    I tried using the command-line tool as per the original docs(https://developers.facebook.com/docs/instagram-basic-display-api/getting-started), but no luck...

    Here's what to do in 3 easy steps:

    1. First thing: Install Postman https://www.postman.com/downloads/
    2. Make a POST request to https://api.instagram.com/oauth/access_token with the parameters in the body, NOT the params. Make sure that the x-www-form-urlencoded option is enable.
    3. You should now get a status of 200 OK and a response with both access_token and user_id.
    {
        "access_token": "IGQVJYUXlDN...",
        "user_id": 17841400...
    }
    

    Happy days!!

    See the screenshot for the correct settings:

    0 讨论(0)
  • 2021-01-04 13:11

    I had this problem when i was trying implement a application.
    My problem was the code generated when you allow the permissions.
    Try to remove #_ from the end of the generated code and try generate the token again

    Generated code example: AQBvrqqBJJTM49U1qTQWRMD96oRyMR3B_04JSfjc-nUIi0iGbSc3x_EceggQi9IyG3B3Rj3ocreMThQoPJbPpeXLUM4exJMy4o01fXcRtT_I9NovaNAqmWSneFt3MYv_k7ifAUUeMlC050n5xnjQP6oAvDBfCFQvTdrFaR95-5i71YsfQlmjYWDG6fcWRvOB9nqr6J9mbGMXMi9Y4tKlSfElaYm0YKRijZQDG2B5PaxQ8A #_

    Generated code edited: AQBvrqqBJJTM49U1qTQWRMD96oRyMR3B_04JSfjc-nUIi0iGbSc3x_EceggQi9IyG3B3Rj3ocreMThQoPJbPpeXLUM4exJMy4o01fXcRtT_I9NovaNAqmWSneFt3MYv_k7ifAUUeMlC050n5xnjQP6oAvDBfCFQvTdrFaR95-5i71YsfQlmjYWDG6fcWRvOB9nqr6J9mbGMXMi9Y4tKlSfElaYm0YKRijZQDG2B5PaxQ8A

    0 讨论(0)
  • 2021-01-04 13:14

    I was also having the same problem, I solved clearing the cache, coockie and other browser data.

    Then I made a new request.

    Try it, it worked with me.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-04 13:18

    I just succeeded by removing the trailing #_ at the end in the code they give you. Not sure if that was your issue?

    https://developers.facebook.com/support/bugs/436837360282557/

    0 讨论(0)
提交回复
热议问题