Impossible to get an access_token for Instagram Basic Display API

放肆的年华 提交于 2020-04-09 18:00:31

问题


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://developers.facebook.com/docs/instagram-basic-display-api/getting-started

But I'm stuck at Step 5: Exchange the Code for a Token

The cURL request always returns a 400 error with the message: "Matching code was not found or was already used"

However, after many tests, I got an access_token one time only, but it expired about one hour later. This seems to be very random.

The Instagram Basic Display API seems rather new. A while ago, I have used apps created on the https://www.instagram.com/developer/ website and it used to work. Now this site display this message:

UPDATE: Starting October 15, 2019, new client registration and permission review on Instagram API platform are discontinued in favor of the Instagram Basic Display API.

... with a link to the developers.facebook.com.


回答1:


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/




回答2:


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




回答3:


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:




回答4:


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.




回答5:


I found the solution.

The direct uri must the same as you use in the beginning.

ex. You use

www.abc.com/auth 

to get the code. When you exchange the token the redirect_uri must be the same as

www.abc.com/auth



回答6:


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);



回答7:


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



来源:https://stackoverflow.com/questions/58489682/impossible-to-get-an-access-token-for-instagram-basic-display-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!