forked from abraham/twitteroauth
-
Notifications
You must be signed in to change notification settings - Fork 2
/
DOCUMENTATION
120 lines (89 loc) · 5.5 KB
/
DOCUMENTATION
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
TwitterOAuth documentation.
GET THE CODE
====================
You can pull the latest development version using git:
git clone git://github.com/abraham/twitteroauth.git
Or you can download the latest release by visiting:
http://github.com/abraham/twitteroauth/downloads
FLOW OVERVIEW
====================
1) Build TwitterOAuth object using client credentials.
2) Request temporary credentials from Twitter.
3) Build authorize URL for Twitter.
4) Redirect user to authorize URL.
5) User authorizes access and returns from Twitter.
6) Rebuild TwitterOAuth object with client credentials and temporary credentials.
7) Get token credentials from Twitter.
8) Rebuild TwitterOAuth object with client credentials and token credentials.
9) Query Twitter API.
TERMINOLOGY
====================
The terminology has changed since 0.1.x to better match the draft-hammer-oauth IETF
RFC. You can read that at http://tools.ietf.org/html/draft-hammer-oauth. Some of the
terms will differ from those Twitter uses as well.
client credentials - Consumer key/secret you get when registering an app with Twitter.
temporary credentials - Previously known as the request token.
token credentials - Previously known as the access token.
PARAMETERS
====================
There are a number of parameters you can modify after creating a TwitterOAuth object.
Switch to XML instead of JSON.
$connection->format = 'xml';
Stop auto decoding JSON.
$connection->decode_json = FALSE;
Custom useragent.
$connection->useragent = 'Custom useragent string';
Verify Twitters SSL certificate.
$connection->ssl_verifypeer = TRUE;
There are several more you can find in TwitterOAuth.php.
EXTENDED FLOW USING EXAMPLE CODE
====================
To use TwitterOAuth with the Twitter API you need TwitterOAuth.php, OAuth.php and
client credentials. You can get client credentials by registering your application at
https://twitter.com/apps.
The example files are explained below.
0) Users start out on connect.php which displays the "Sign in with Twitter" image hyperlinked
to redirect.php. This button should be displayed on your homepage in your login section. The
client credentials are saved in config.php as CONSUMER_KEY and CONSUMER_SECRET. You can
save a static callback URL in the app settings page, in the config file or use a dynamic
callback URL later in step 2. In example use http://example.com/callback.php.
1) When a user lands on redirect.php we build a new TwitterOAuth object using the client
credentials. If you have your own configuration method feel free to use it instead of config.php.
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);// Use config.php client credentials
$connection = new TwitterOAuth('abc890', '123xyz');
2) Using the built $connection object you will ask Twitter for temporary credentials. If you
wish to have a dynamic callback URL for each user you can do pass a URL as a parameter.
$temporary_credentials = $connection->getRequestToken(); // Use applications registered callback.
$temporary_credentials = $connection->getRequestToken('http://example.com/callback.php?');
3) Now that we have temporary credentials the user has to go to Twitter and authorize the app
to access and updates their data. You can also pass a second parameter of FALSE to not use Sign
in with Twitter: http://apiwiki.twitter.com/Sign-in-with-Twitter.
$redirect_url = $connection->getAuthorizeURL($temporary_credentials); // Use Sign in with Twitter
$redirect_url = $connection->getAuthorizeURL($temporary_credentials, FALSE);
4) You will now have a Twitter URL that you must send the user to. You can add parameters and
they will return with the user in step 5.
https://twitter.com/oauth/authenticate?oauth_token=xyz123
https://twitter.com/oauth/authenticate?oauth_token=xyz123&info=abc // info will return with user
5) The user is now on twitter.com and may have to login. Once authenticated with Twitter they will
will either have to click on allow/deny, or will be automatically redirected back to the callback.
6) Now that the user has returned to callback.php and allowed access we need to build a new
TwitterOAuth object using the temporary credentials.
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_SESSION['oauth_token'],
$_SESSION['oauth_token_secret']);
7) Now we ask Twitter for long lasting token credentials. These are specific to the application
and user and will act like password to make future requests. If a dynamic callback URL was used
you will also have to pass the oauth_verifier parameter. Normally the token credentials would
get saved in your database but for this example we are just using sessions.
$token_credentials = $connection->getAccessToken(); // Used applications registered callback URL
$token_credentials = $connection->getAccessToken($_REQUEST['oauth_verifier']);
7a) After getting the token credentials we redirect the user to index.php.
8) With the token credentials we build a new TwitterOAuth object.
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $token_credentials['oauth_token'],
$token_credentials['oauth_token_secret']);
9) And finally we can make requests authenticated as the user. You can GET, POST, and DELETE API
methods. Directly copy the path from the API documentation and add an array of any parameter
you wish to include for the API method such as curser or in_reply_to_status_id.
$content = $connection->get('account/verify_credentials');
$connection->post('statuses/update', array('status' => 'Text of status here',
'in_reply_to_status_id' => 123456));
$content = $connection->delete('statuses/destroy/12345');