PHP Classes

OAuth Twitter versus database token

Recommend this page to a friend!

      PHP OAuth Library  >  All threads  >  OAuth Twitter versus database token  >  (Un) Subscribe thread alerts  
Subject:OAuth Twitter versus database token
Summary:Retrieve token from database and reuse it
Messages:8
Author:Vincent
Date:2013-06-25 15:22:58
Update:2013-07-08 10:20:48
 

  1. OAuth Twitter versus database token   Reply   Report abuse  
Picture of Vincent Vincent - 2013-06-25 15:22:58
Hello,

First of all thank you for your great package of oauth api files. I tried connecting to Twitter and it works like a charm! :)

Now I prefer saving the oauth token to a mysql database and reuse the token. I found an example file 'mysqli_login_with_google.php' but I can't make it work on Twitter.

I've edited 'login_with_twitter.php'. I added the following lines:

11. require('database_oauth_client.php');
12. require('mysqli_oauth_client.php');
13. $client = new mysqli_oauth_client_class; // replaced: $client = new oauth_client_class;
14. $client->database = array(
15. 'host'=>'XXX',
16. 'user'=>'XXX',
17. 'password'=>'XXX',
18. 'name'=>'XXX',
19. 'port'=>0,
20. 'socket'=>'/var/lib/mysql/mysql.sock'
21. );

After executing 'http://www.mydomain.com/test/login_with_twitter.php' the Twitter application authorization shows up. After authorizing I receive the following message:

OAuth client error
Error: it was not possible to access the API call: it was returned an unexpected response status 401 Response: {"errors":[{"message":"Invalid or expired token","code":89}]}

The mysql database contains a new record containing the new generated twitter data. Saving to the database works.

I'm not sure what to do next. Can you please inform me what's wrong?

Thank you in advance for your help.

With kind regards,

Vincent

  2. Re: OAuth Twitter versus database token   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2013-07-02 05:26:04 - In reply to message 1 from Vincent
Sorry for the delay.

Yes, there was a bug that prevented correct decoding a boolean values stored in the database.

The bug is fixed and the new version was uploaded. Just let me know if you find any other problems.

  3. Re: OAuth Twitter versus database token   Reply   Report abuse  
Picture of Vincent Vincent - 2013-07-04 11:44:24 - In reply to message 2 from Manuel Lemos
Thank you for your answer and update. I'll try out the new update and let you know if everything works properly.

  4. Re: OAuth Twitter versus database token   Reply   Report abuse  
Picture of Vincent Vincent - 2013-07-05 13:57:32 - In reply to message 3 from Vincent
I tried the latest version. It works properly without errors. Still I can't figure out how to reuse the database token. After authorizing my application to Twitter a database record is added into my database and '/statuses/home_timeline.json' is properly called.

What steps do I need to take now to reuse the tokens from the database? I tried adding '$client->user = 1;' and '$client->offline = true;'. After authorizing the application, I receive the message 'OAuth client error - Error: it was not found the OAuth session for user 1'. (Index/User 1 is correct, there is only one database record)

What I need:
- One time authorizing my application
- Save tokens to database
- Reuse tokens from database for handling Twitter API
- Reauthorize my application when tokens are out of date

Can you please help me out? I'm stuck and put hours in a solution. I like your package because you support all major social media APIs. Thank you in advance for your help.

  5. Re: OAuth Twitter versus database token   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2013-07-06 11:47:18 - In reply to message 4 from Vincent
There was a bug that was preventing to set the access token secret. The bug is fixed and the new version was just uploaded. Just let me know if that does not address your problem.

By the way, as far as I know, for Twitter the tokens do not seem to expire ever, unless they are revoked.

  6. Re: OAuth Twitter versus database token   Reply   Report abuse  
Picture of Vincent Vincent - 2013-07-08 08:53:25 - In reply to message 5 from Manuel Lemos
Dear Manuel,

I understand that the bug is fixed. My problem is how to use the client for retrieving the token from the database for communicating with the Twitter API. Do you have an example?

Thank you for your help. :)

With kind regards,

Vincent

  7. Re: OAuth Twitter versus database token   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2013-07-08 09:09:03 - In reply to message 6 from Vincent
You do not have to retrieve the token from the application in your application. Just use the database and MySQLi sub-classes and they retrieve it on demand when you call the CallAPI function.

Take a look at the mysqli_offline_access_to_twitter.php example script.

  8. Re: OAuth Twitter versus database token   Reply   Report abuse  
Picture of Vincent Vincent - 2013-07-08 10:20:48 - In reply to message 7 from Manuel Lemos
Thank you for your answer. You're right. I need 'mysqli_offline_access_to_twitter.php' for handling the tokens from the database. I missed that one. ;)

(The file name was confusing to me. In my opinion it should called something like 'mysqli_client_twitter.php')

Thank you again!