symphonycms / remote_datasource

An improved datasource to fetch data from TXT, CSV, XML and JSON sources.
Other
14 stars 9 forks source link

oAuth #19

Closed nilshoerrmann closed 10 years ago

nilshoerrmann commented 10 years ago

I'd like to fetch data from Twitter: is there a way to let this extension handle authorisation via oAuth?

nilshoerrmann commented 10 years ago

I noticed this comment and function in the remote data source code:

    /**
     * This methods allows custom remote data source to set other
     * properties on the HTTP gateway, like Authentication or other
     * parameters. This method is call just before the `exec` method.
     *
     * @param Gateway $gateway
     *  the Gateway object that will be use for the current HTTP request
     *  passed by reference
     */
    public static function prepareGateway(&$gateway) {}

@brendo: Is there an example how to work with gateway here, e. g. how to add oAuth authorisation in this place. And is it actually correct to do that here?

nilshoerrmann commented 10 years ago

Okay, it's the right place and it works if one adds all authorisation credentials to $gateway here as described on Twitter's developer site.

brendo commented 10 years ago

Sorry, yep, that's what @nitriques created the delegate for :)

On Mon, Jun 23, 2014 at 8:55 PM, Nils Hörrmann notifications@github.com wrote:

Closed #19 https://github.com/symphonycms/remote_datasource/issues/19.

— Reply to this email directly or view it on GitHub https://github.com/symphonycms/remote_datasource/issues/19#event-134036685 .

nilshoerrmann commented 10 years ago

One silly thing I noticed: I cannot access the URL of the request in prepareGateway because it's a static function. $gateway contains all the settings, but doesn't offer a way to access these private values.

nitriques commented 10 years ago

@nilshoerrmann Here's how I did it:


public $dsParamURL = 'https://api.twitter.com/1.1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=%s&count=1';
public $dsParamFORMAT = 'json';
public $dsParamXPATH = '/';
public $dsParamCACHE = 0;
public $dsParamTIMEOUT = 10; 

public function prepareGateway($ch) {
            $ch->setopt('HTTPHEADER',array('Content-Type: application/x-www-form-urlencoded;charset=UTF-8'));
            $ch->setopt('HTTPHEADER',array('Authorization: Bearer AAAAAAAAAAAAAAAAAAAA...'));
        }

To create the bearer-token, please visit: http://twitter-bearer-token.herokuapp.com/

nilshoerrmann commented 10 years ago

Interesting. That's a lot easier than what I did. Thanks a lot, Nicolas!

nitriques commented 10 years ago

You're welcome! BTW, this uses the application authentication scheme. (see https://dev.twitter.com/docs/auth/application-only-auth) If you need to identify as a real user, let me know (I think I have code for this too).

nilshoerrmann commented 10 years ago

If you need to identify as a real user, let me know (I think I have code for this too).

From what I've read on the Twitter site, I thought I'd have to identify as a real user. What's the difference between the two approaches? I'd just like to fetch the latest 20 entries from a user timeline.

nitriques commented 10 years ago

I do not think you need to be auth as a real user for that (If the tweets are public). Authenticating as a real user will allow you to do request on the user's behalf: like tweeting under its name, following new accounts and getting the account followed by the user.

In order to do that, you will need to implement a complete oAuth solution. If you need one, I could sent you some code that will do it (it supports both twitter and facebook).

If you only want to fetch tweets from a public account, creating a application (https://apps.twitter.com/app/new) and usign the bearer token should be enough. The great thing about the bearer token is that it never expires.

nilshoerrmann commented 10 years ago

Great, thanks a lot!

nitriques commented 10 years ago

My pleasure.