espresso-dev / instagram-basic-display-php

A simple PHP class for accessing the Instagram Basic Display API
MIT License
112 stars 23 forks source link

Authentication error #5

Closed Vivatec closed 4 years ago

Vivatec commented 4 years ago

Hello,

When trying to authenticate the user (OAuth2), I get the following error message: Fatal error: "Uncaught Error: Call to a member function getOAuthToken () on null in" Refers to that line: $token = $instagram->getOAuthToken($code, true);

Do you have any idea what could be causing this? Thank you

hermanschutte commented 4 years ago

Did you initialize the class?

use EspressoDev\InstagramBasicDisplay\InstagramBasicDisplay;

$instagram = new InstagramBasicDisplay([
    'appId' => 'YOUR_APP_ID',
    'appSecret' => 'YOUR_APP_SECRET',
    'redirectUri' => 'YOUR_APP_REDIRECT_URI'
]);
Vivatec commented 4 years ago

Yes. I reported the three data that were taken from developers.facebook. After clicking the link he is redirected to the Instagram confirmation page. I click on confirm and go back to my website, however with that error message.

I thank you for your help and sorry for the bad English.

hermanschutte commented 4 years ago

Could you share the code you use to get the oauth token?

Vivatec commented 4 years ago

It includes a link with the image of the codes of the two pages that are used.

https://iboy.com.br/error.php

The code is exactly what you suggested in the examples.

hermanschutte commented 4 years ago

Yes, it happens because you are not instantiating an instance of the class in the redirect URL.

You will need to add this to your redirect page code:

$instagram = new InstagramBasicDisplay([
    'appId' => 'YOUR_APP_ID',
    'appSecret' => 'YOUR_APP_SECRET',
    'redirectUri' => 'YOUR_APP_REDIRECT_URI'
]);
Vivatec commented 4 years ago

That's exactly what I'm doing.

require __DIR__ . '/vendor/autoload.php'; use EspressoDev\InstagramBasicDisplay\InstagramBasicDisplay; $instagram = new InstagramBasicDisplay([ 'appId' => '000000000', 'appSecret' => 'xxxxxxxxxxx', 'redirectUri' => 'https://iboy.com.br/auth.php' ]); echo "<a href='{$instagram->getLoginUrl()}'>Login with Instagram</a>";

After the Instagram confirmation screen I am directed to the page where the error occurs.

The error occurs in this second step: https://iboy.com.br/auth.php?code=XXXXXXXXXXXXX...

require __DIR__ . "/vendor/autoload.php"; use EspressoDev\InstagramBasicDisplay\InstagramBasicDisplay; $code = $_GET['code']; $token = $instagram->getOAuthToken($code, true); // the error occurs on that line echo "Your token is: " . $token;

hermanschutte commented 4 years ago

Where on the page that the error occurs are you instantiating the class? I'm not seeing in your screenshot.

You need to add this to that page:

$instagram = new InstagramBasicDisplay([ 'appId' => '000000000', 'appSecret' => 'xxxxxxxxxxx', 'redirectUri' => 'https://iboy.com.br/auth.php' ]);

I'm not sure how else to make this clear to you.

Vivatec commented 4 years ago

I'm sorry for the stupidity. There was no understand that it was to insert in the other file too.

It worked.

Thank you for your help