jsmrtn / craftagram

Grab Instagram content through the Instagram Basic Display API
Other
14 stars 8 forks source link

Error Refreshing the Access Token #35

Closed AnsonSL closed 3 years ago

AnsonSL commented 3 years ago

Hi, just attempted refreshing the access token for the first time and I'm running into an issue.

  1. When I go to refresh the token the redirect_uri doesn't include the domain despite the domain being included in Valid OAuth Redirect URIs in the Facebook developer panel. &redirect_uri=/actions/craftagram/default/auth&scope=user_profile,user_media&response_type=code I get around this by modifying the query string above and adding the domain of the site to redirect_uri &redirect_uri=mydomain.com/actions/craftagram/default/auth&scope=user_profile,user_media&response_type=code

  2. Once I press return I'm shown the Instagram screen asking me to confirm that I'd like to continue using my previous settings.

  3. Instagram then redirects back to the auth action on my website (mydomain.com/actions/craftagram/default/auth?code={token})

Undefined property: stdClass::$access_token in .../vendor/scaramangagency/craftagram/src/services/CraftagramService.php

curl_setopt($ch, CURLOPT_URL,'https://api.instagram.com/oauth/access_token');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$res = curl_exec($ch);
curl_close($ch);

**$shortAccessToken = json_decode($res)->access_token;**

return Craftagram::$plugin->craftagramService->getLongAccessToken($shortAccessToken);

Any suggestions?

jsmrtn commented 3 years ago

What version of the plugin are you using? The URL you have copied is a different format to how it presents now. Presumably this is happening when you hit 'Authorise Craft' button?

As an aside, there is a refresh action built in, you can run ./craft craftagram/token from the CLI to refresh your token, or you can visit the action URL directly https://www.yourwebsite.com/actions/craftagram/default/refresh-token (note you will need to update to 1.4.2, as this contains a fix for the refresh action)

AnsonSL commented 3 years ago

Thanks @joshua-martin the dev site is running 1.3.1 so I will attempt updating that first and get back to you.

AnsonSL commented 3 years ago

Hi @joshua-martin

  1. Gave the upgraded version a try and I run into the same issue. The redirect_uri still doesn't include the site url so it's getting stuck.

https://www.instagram.com/oauth/authorize?client_id=12324532432432&scope=user_profile,user_media&response_type=code&redirect_uri=/actions/craftagram/default/auth&state=1

{"error_type": "OAuthException", "code": 400, "error_message": "Invalid redirect_uri"}

When I add the site name it directs back to Craft. But then I get the same error as before (line 106 this time).

Undefined property: stdClass::$access_token

  1. Tried the action URL directly and the CLI option and I get a different error: Calling unknown method: craft\services\Sites::allSiteIds()

This Craft install is a little behind. Could that be the issue?

Thanks!

jsmrtn commented 3 years ago

Have you installed 1.4.2? The issue you have mentioned in 2. should be resolved in that release.

What version of Craft are you running? It looks like there is no value being added to the baseUrl parameter for your primary website. Have you added a baseUrl Settings > Sites, and if so what is it set to?

Sent on the move. Please excuse any errors.


From: AnsonSL notifications@github.com Sent: Wednesday, January 6, 2021 10:13:49 PM To: scaramangagency/craftagram craftagram@noreply.github.com Cc: Josh Martin josh@Scaramanga.agency; Mention mention@noreply.github.com Subject: Re: [scaramangagency/craftagram] Error Refreshing the Access Token (#35)

Hi @joshua-martinhttps://github.com/joshua-martin

  1. Gave the upgraded version a try and I run into the same issue. The redirect_uri still doesn't include the site url so it's getting stuck.

https://www.instagram.com/oauth/authorize?client_id=12324532432432&scope=user_profile,user_media&response_type=code&redirect_uri=/actions/craftagram/default/auth&state=1

{"error_type": "OAuthException", "code": 400, "error_message": "Invalid redirect_uri"}

When I add the site name it directs back to Craft. But then I get the same error as before (line 106 this time).

Undefined property: stdClass::$access_token

  1. Tried the action URL directly and the CLI option and I get a different error: Calling unknown method: craft\services\Sites::allSiteIds()

This Craft install is a little behind. Could that be the issue?

Thanks!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/scaramangagency/craftagram/issues/35#issuecomment-755747658, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAM5Z3LAAGSZFB4GF5SMU7TSYTOB3ANCNFSM4VXZBP2A.

AnsonSL commented 3 years ago

@joshua-martin Thanks for your help. Updating everything has helped, and the issue was the baseUrl setting was set to blank for the primary domain on a multilingual site.

Question: does the CRON job refresh tokens for all sites? /usr/bin/wget -q https://sitename.com/actions/craftagram/default/refresh-token >/dev/null 2>&1

jsmrtn commented 3 years ago

No worries, glad you got it sorted. Yes, the refresh job will attempt to run against every site. If there is no access token defined for a site it will skip.