OAuth authentication for access token acquisition is not working

Asked 2 years ago, Updated 2 years ago, 153 views

We are developing PHP using the Instagram API in our local environment.When authenticating the application for access token acquisition, if you skip the URL to Instagram from the login screen, the following error will appear.
The authentication screen should be up, but the wrong part cannot be considered and the reference for solving the error cannot be found.
Can someone give me some advice?Thank you for your cooperation.

Error:

{
    "code"—400,
    "error_type": "OAuthException",
    "error_message": "You must include a valid client_id, response_type, and redirect_uri parameters"
}

URL returned by browser:

https://instagram.com/oauth/authorize/?client_id=[CLIENT_ID] redirect_uri=http://insta.com/htdocs/redirect.php&%26scope=basic%2Bcomments%2Brelationships%2Blikes&%26response_type=code

session_start();
if(empty($_GET['code']))}
  // Pre-Authentication Preparation
  $params=array(
  'client_id' = > CLIENT_ID,
  '&redirect_uri' = > SITE_URL. 'htdocs/redirect.php',
  '&scope' = > 'basic + comments + relationships + likes',
  '&response_type' = > 'code'
  );
  $url='https://api.instagram.com/oauth/authorize/?'.http_build_query($params);

  // post on Instagram
  header('Location:'.$url);
  exit;

php api oauth instagram

2022-09-30 17:14

2 Answers

How about the following?

//Pre-authentication Preparation
 $params=array(
     'client_id' = > CLIENT_ID,
     'redirect_uri' = > urlencode (SITE_URL.'htdocs/redirect.php'),
     'scope' = > 'basic comments relationships likes',
     'response_type' = > 'code'
 );
 $url='https://api.instagram.com/oauth/authorize/?'.http_build_query($params);

 // post on Instagram
 header('Location:'.$url);
 exit;


2022-09-30 17:14

I think the array I gave to http_build_query was automatically added without using & or +.


2022-09-30 17:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.