Home > Blockchain >  Instagram Basic Display API does not return profile for test users
Instagram Basic Display API does not return profile for test users

Time:10-14

I am trying to get instagram username for test users with the instagram basic display API.

So far I have successfully coded to get the oAuth Consent, get access token and to get the long lived access token.

When I make the get username instagram basic api call, it returns the following error:

{"error":{"message":"Unsupported get request. Object with ID '1.7841411138E 16' does not exist, cannot be loaded due to missing permissions, or does not support this operation","type":"IGApiException","code":100,"error_subcode":33,"fbtrace_id":"APM8hPTe1xn1OfjF9GLfHnm"}}

I also noticed the instagram id contains a ' ' and a '.'.

Any help is greatly appreciated.

This is my code:

//Request

 public function oAuthBasic() {
        $instagramBasic = new InstagramBasicDisplay([
            'appId' => Config::get('instagram_basic.app_id'),
            'appSecret' => Config::get('instagram_basic.app_secret'),
            'redirectUri' => Config::get('instagram_basic.redirect_uri')
        ]);

        session()->forget('instagramErrorMessage');
        $faceBookLoginUrl = $instagramBasic->getLoginUrl(['user_profile', 'user_media']);
        return response()->json(['redirectUrl' => $faceBookLoginUrl]);
 }


//Call Back

public function linkBasic(InstagramLinkRequest $instagramRequest) {
        
        if (isset($_GET['code'])) {
            // Get the OAuth callback code
            $code = $_GET['code'];
            $ig_atu = "https://api.instagram.com/oauth/access_token";
            $ig_data = [];
            $ig_data['client_id'] = Config::get('instagram_basic.app_id');
            $ig_data['client_secret'] = Config::get('instagram_basic.app_secret');
            $ig_data['grant_type'] = 'authorization_code';
            $ig_data['redirect_uri'] = Config::get('instagram_basic.redirect_uri');
            $ig_data['code'] = $code;

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $ig_atu);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($ig_data));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $ig_auth_data = curl_exec($ch);
            curl_close($ch);
            $ig_auth_data = json_decode($ig_auth_data, true);

            $access_token = $ig_auth_data['access_token'];
            $user_id = $ig_auth_data['user_id'];
            //dd($access_token);
            
            
            try{
                //get long lived access token
                $url = 'https://graph.instagram.com/access_token/?';
                $data = array(
                            'client_secret' => Config::get('instagram_basic.app_secret'),
                            'access_token'  => $access_token,
                            'grant_type'    => 'ig_exchange_token'
                );
                
                $string = http_build_query($data);
                $ch = curl_init($url.$string);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                $resultRow = curl_exec($ch);
                $result = json_decode($resultRow);
                $long_lived_access_token = $result->access_token;
                curl_close($ch);
                //dd($long_lived_access_token);
                
                 //get user profile
                 $url = 'https://graph.instagram.com/'.$user_id.'/?';
                 $data = array('access_token'  => $long_lived_access_token, 'fields'=> 'username');
                 $string = http_build_query($data);
                 $ch = curl_init($url.$string);
                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                 $result = curl_exec($ch);
                 curl_close($ch);
                 dd($result);
            }catch (Exception $e){
                dd($e->getMessage());
            }
            
        }
    }

@CBroe

CodePudding user response:

I also noticed the instagram id contains a ' ' and a '.'."_

That is actually the scientific notation of a floating-point number.

This is the result of treating a value that is larger than the maximum integer value PHP can handle, as a number.

Use the JSON_BIGINT_AS_STRING flag in your json_decode call to make it decode this into a string value, instead of a number.

  • Related