0

My problem is I get Instagram id and username, but I will need full name, profile image, email etc. I get like this Array:

( [id] => 123456 [username] => abc123)

$code = $this->getRequest()->getParam('code');
        $instaAppId = $this->modelDataProvider->getAppApiKey(self::SOCIAL_TYPE);
        $instaAppSecret = $this->modelDataProvider->getAppApiSecret(self::SOCIAL_TYPE);
        $instaRedirectUrl = $this->modelDataProvider->getRedirectCallbackUrls(self::SOCIAL_TYPE);
        $setAccessToken = $this->getAccessTokenValue($instaAppId, $instaRedirectUrl, $instaAppSecret, $code);
        $AccessToken = $setAccessToken['access_token'];
        $instauserinfo = $this->getInstagramUserProfile($AccessToken);
        print_r($instauserinfo);


protected function getAccessTokenValue($instaAppId, $instaRedirectUrl, $instaAppSecret, $code)
    {
        $token = [];
        try {
            $request = 'client_id='. $instaAppId . '&redirect_uri=' . $instaRedirectUrl . '&client_secret=' . $instaAppSecret . '&code='. $code . '&grant_type=authorization_code';
            $this->curlClient->setOptions([
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_TIMEOUT => 60,
                CURLOPT_POSTFIELDS => $request,
                CURLOPT_HEADER => false,
                CURLOPT_VERBOSE => true,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_SSL_VERIFYPEER => false,
                CURLOPT_HTTPHEADER => [
                    'Content-Type: application/x-www-form-urlencoded',
                ]
            ]);
            $this->curlClient->get('https://api.instagram.com/oauth/access_token');
            $token = $this->json->unserialize($this->curlClient->getBody());
        } catch (\Exception $exception) {
            $token['errorMessage'] = $exception->getMessage();
        }
        return $token;
    }

protected function getInstagramUserProfile($AccessToken)
    {
        $instaprofileid = [];
        try {
            $this->curlClient->setOptions([
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_TIMEOUT => 60,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_SSL_VERIFYPEER => false,
                CURLOPT_HTTPHEADER => [
                    'Authorization: Bearer ' . $AccessToken,
                ]
            ]);
            $this->curlClient->get('https://graph.instagram.com/me/?fields=id,name,username,profile_picture_url&access_token='. $AccessToken);
            $instaprofileid = $this->json->unserialize($this->curlClient->getBody());
        } catch (\Exception $exception) {
            $this->getResponse()->setBody($exception->getMessage());
        }
        return $instaprofileid;
    }

I get only id and username. I need all info from instagram .

I try to social login plugin.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • https://developers.facebook.com/docs/instagram-basic-display-api/reference/me points to, https://developers.facebook.com/docs/instagram-basic-display-api/reference/user which says you can request the following fields about a user (including yourself): account_type, id, media_count, username. It's not clear why you think you'd get more. You can also get edge data about media such as images, and then query them for more info with (what I think would be) a separate request: https://developers.facebook.com/docs/instagram-basic-display-api/reference/media/ – ADyson Jul 14 '23 at 09:56
  • The rest of the data not mentioned in the docs (such as email and whatever else you were interested in) is considered private by instagram and isn't provided via the API – ADyson Jul 14 '23 at 10:00
  • ok. but how to get full name? – Rubel Parvaz Jul 14 '23 at 10:20
  • If it's not available in the API, as documented, then you can't. It's not a public part of a user's profile in the API. Complain to Meta if you don't like it. – ADyson Jul 14 '23 at 10:31
  • https://stackoverflow.com/a/61555402/5947043 suggests an alternative route but I have no idea if it would still work – ADyson Jul 14 '23 at 10:37
  • $url = "https://instagram.com/{$username}/?__a=1"; $response = file_get_contents($url); $data = json_decode($response, true); print_r($data); – Rubel Parvaz Jul 14 '23 at 10:43
  • no output. this array is empty – Rubel Parvaz Jul 14 '23 at 10:44
  • What makes you expect that `$response` contains JSON? Did you actually check? – ADyson Jul 14 '23 at 10:44
  • no. I do not check – Rubel Parvaz Jul 14 '23 at 10:53
  • i try this but some error Unable to unserialize value. Error: Syntax error $username = $instauserinfo['username']; $this->curlClient->get('https://instagram.com/'.$username.'/?__a=1'); $instaproinfo = $this->json->unserialize($this->curlClient->getBody()); print_r($instaproinfo); – Rubel Parvaz Jul 14 '23 at 10:57
  • That still seems to assume the response is JSON! We don't know if it definitely is. The failure suggests that it probably isn't JSON. Check the _raw_ response first, so you know what you're dealing with. In your first version of that file_get_contents code, just use `var_dump($response);` to debug it. – ADyson Jul 14 '23 at 11:02
  • echo var_dump($response); output string(0) "" – Rubel Parvaz Jul 14 '23 at 11:09
  • So an empty response. I did say I would be surprised if it works. You can google and see if anyone else has any suggestions, but I am pretty certain it is not possible. – ADyson Jul 14 '23 at 12:04

0 Answers0