phpfui / ConstantContact

MIT License
16 stars 7 forks source link

Empty tokens returned #18

Closed almirramic closed 8 months ago

almirramic commented 8 months ago

I think I do everything right, the verification returns to my redirect URI, with values in the 'code' and 'state' _GET vars. But the getAuthorizationURL() function does not seem to update the $client class with accessToken and refreshToken, or Constant Contact does not return them or returns empty values. In any case, accessToken and refreshToken are empty. No errors are reported either.

I am on PHP 8.1.21.

Please help.

phpfui commented 8 months ago

Can you supply source code?

Thanks!

almirramic commented 8 months ago

Can you supply source code?

Thanks!

$client = new \PHPFUI\ConstantContact\Client($CCAPIKey, $CCSecret, $CCRedirectURL);
// set any scopes here, defaults to all scopes. Your user will need to accept what ever scopes you specify.
// $client->setScopes(['contact_data', 'campaign_data']);
\header('location: ' . $client->getAuthorizationURL());

$CCAPIKey, $CCSecret, $CCRedirectURL are my variables from db. Checked and rechecked multiple times.

The app goes to the authorization link and returns this _GET on the redirect URL:

?code=vNFtcX_1Y0aM0v14beiOsP8BITKk_wOlcNn-rUh3jRU&state=fe7fdddc43f58fc0

where I do:

$client = new \PHPFUI\ConstantContact\Client($CCAPIKey, $CCSecret, $CCRedirectURL);
$client->acquireAccessToken($_GET);

The class produces no errors of any kind and $client->lastError is empty. No PHP errors either. The class also has the right ids, secret, etc.

Also checked and rechecked things in app definition at app.constantcontact.com.

phpfui commented 8 months ago

After your call to acquireAccessToken(), check $client->accessToken and $client->refreshToken. They should be set. Save them to your database, as you will need them to access the API going forward.

Let me know if not.

almirramic commented 8 months ago

After your call to acquireAccessToken(), check $client->accessToken and $client->refreshToken. They should be set. Save them to your database, as you will need them to access the API going forward.

Let me know if not.

That is the problem. Like I said, it all seems to work right, except $client->accessToken and $client->refreshToken end up being empty strings.

phpfui commented 8 months ago

It could be your session is not set up correctly, or a curl error. Both should return some sort of error that you should see by calling getLastError(). I have had to set up some sort of certificates on my local machine to make curl work in the past.

I would look into the exec method in the client. It is doing most of the work in there.

Let me know what you find and I can add some sort of error logging for it, so the next person who ends up with your error has a better error message.

almirramic commented 8 months ago

OK. Thanks. I’ll check and report back.

phpfui commented 8 months ago

Awesome. If you want, you could submit a PR for anything you find. See my article on submitting a PR. Gives you all the steps if you have not done one before.

Thoughts on PHP Blog - Your first PR

almirramic commented 8 months ago

It could be your session is not set up correctly, or a curl error. Both should return some sort of error that you should see by calling getLastError(). I have had to set up some sort of certificates on my local machine to make curl work in the past.

I would look into the exec method in the client. It is doing most of the work in there.

Let me know what you find and I can add some sort of error logging for it, so the next person who ends up with your error has a better error message.

After digging through the code and testing the curl in Client::acquireAccessToken(), I found out that the curl call returned 400 Bad Request. I looked up if others faced the same issue and found this:

https://community.constantcontact.com/t5/API-Developer-Support/400-Bad-Request-when-getting-access-token/td-p/400765

So in the Client::acquireAccessToken() function, just before $this->setAuthorization($ch); I added this line:

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['client_id' => $this->clientID, 'client_secret' => $this->clientSecret, 'code' => $_GET['code']]) );

This solved the problem. Not sure why, but i am sure you will find it valuable, especially knowing others have faced the same problem.

almirramic commented 8 months ago

I’ll try that. As soon as I catch up. :)