phpfui / ConstantContact

MIT License
16 stars 7 forks source link

Token vars blank after calling RefreshToken #5

Closed degive closed 2 years ago

degive commented 2 years ago

I set the $client->accessToken & client->$refreshToken vars then do a refreshTocken() and the $client->accessToken & $client->refreshToken are blank after that?

Any Idea why?

phpfui commented 2 years ago

Can you post your code?

Initial setup is complex. Make sure you have sessions enabled. Follow all instructions in readme.

degive commented 2 years ago

I did follow the readme and have session on, here is the code. Thank you for your help!

session_start(); include("ConstantContact/vendor/autoload.php");

//Constant Contact v3 $apiKey=".................."; $secret="............"; $url="https://rpat.org/includes/cc_api_auth.php";

// Create a client $client = new \PHPFUI\ConstantContact\Client($apiKey, $secret, $url);

//First Time Save Tokens $auth=XSS2("auth"); if ($auth!="") { header('location: ' . $client->getAuthorizationURL()); exit(); }

// Set access and refresh tokens on client $sql="SELECT * FROM cc_auth WHERE id=1"; $result = MySqliQuery($sql); if ($rs = MySqliFetchArray($result)) { $client->accessToken = $rs["code"]; $client->refreshToken = $rs["state"]; echo "Cur Auth: ".$client->accessToken." | ".$client->refreshToken."
"; }

// Refresh the tokens.
$res=$client->refreshToken(); echo $res."
"; echo "New Auth: ".$client->accessToken." | ".$client->refreshToken."
";

Here is the output of the run: Cur Auth: b5_P2V0uYtiu9fVlw7qsGTYJuk9oOO1m7hFWmzcUEuU | 246713f845f5c945 New Auth: |

phpfui commented 2 years ago

I think your basic problem is you are trying to combine two different functions into one file. You need to authorize first. This is a different control flow from usage.

Basically:

$client = new \PHPFUI\ConstantContact\Client($apiKey, $secret, $tokenURL);
\header('location: ' . $client->getAuthorizationURL());

In the $tokenURL file you need to do the following:

$client = new \PHPFUI\ConstantContact\Client($apiKey, $secret, $tokenURL);
$client->acquireAccessToken($_GET);
// Save $client->accessToken and $client->refreshToken to the database

The above only needs to be done once (or if you are not authorized due to credentials being revoked or expiring).

Then you should be able to use the $client in another page with the first code in the readme.

You will probably also want to allow for deauthorizing. Basically you need to give your user the ability to authorize (give your app permissions) and deauthorize if they want to turn it off.

I would also recommend printing out error messages and status on each step, as I think the error messages are fairly informative.

I have done a more complex authorization view, but it relies on a the PHPFUI library and my internal OO database objects that is probably beyond what your are trying to do. But happy to share if you want to dive into it. Not sure it would fit into your site as it is a View in an MVC system.

degive commented 2 years ago

Thank you. This works!

phpfui commented 2 years ago

Awesome!