You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+255-7Lines changed: 255 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,39 @@
1
+
OAuth.io JavaScript SDK
2
+
=======================
3
+
4
+
This is the JavaScript SDK for [OAuth.io](https://oauth.io). OAuth.io allows you to integrate **100+ providers** really easily in your web app, without worrying about each provider's OAuth specific implementation.
5
+
1
6
Installation
2
7
============
3
8
9
+
Getting the SDK
10
+
---------------
11
+
12
+
To get the SDK, you can :
13
+
14
+
- download the zip file from this repository
15
+
- get it via Bower
16
+
17
+
**Zip file**
18
+
19
+
Just copy the dist/oauth.js or dist/oauth.min.js to your project.
20
+
21
+
**Bower**
22
+
23
+
```sh
24
+
$ bower install oauth-js
25
+
```
26
+
27
+
Integrating in your project
28
+
---------------------------
29
+
4
30
In the `<head>` of your HTML, include OAuth.js
5
31
6
-
`<script src="/path/to/OAuth.js"></script>`
32
+
`<script src="/path/to/oauth.js"></script>`
7
33
8
34
In your Javascript, add this line to initialize OAuth.js
9
35
10
-
`OAuth.initialize('Public key');`
36
+
`OAuth.initialize('your_app_public_key');`
11
37
12
38
Usage
13
39
=====
@@ -19,9 +45,13 @@ Mode popup
19
45
20
46
```javascript
21
47
//Using popup (option 1)
22
-
OAuth.popup('facebook', function(err, result) {
48
+
OAuth.popup('facebook')
49
+
.done(function(result) {
50
+
//use result.access_token in your API request
51
+
//or use result.get|post|put|del|patch|me methods (see below)
//or use result.get|post|put|del|patch|me methods (see below)
73
+
})
74
+
.fail(function (err) {
75
+
//handle error with err
42
76
});
43
77
```
44
78
79
+
Making requests
80
+
---------------
81
+
82
+
You can make requests to the provider's API manually with the access token you got from the `popup` or `callback` methods, or use the request methods stored in the `result` object.
83
+
84
+
**GET Request**
85
+
86
+
To make a GET request, you have to call the `result.get` method like this :
87
+
88
+
```javascript
89
+
//Let's say the /me endpoint on the provider API returns a JSON object
90
+
//with the field "name" containing the name "John Doe"
91
+
OAuth.popup(provider)
92
+
.done(function(result) {
93
+
result.get('/me')
94
+
.done(function (response) {
95
+
//this will display "John Doe" in the console
96
+
console.log(response.name);
97
+
})
98
+
.fail(function (err) {
99
+
//handle error with err
100
+
});
101
+
})
102
+
.fail(function (err) {
103
+
//handle error with err
104
+
});
105
+
```
106
+
107
+
**POST Request**
108
+
109
+
To make a POST request, you have to call the `result.post` method like this :
110
+
111
+
```javascript
112
+
//Let's say the /message endpoint on the provider waits for
113
+
//a POST request containing the fields "user_id" and "content"
114
+
//and returns the field "id" containing the id of the sent message
115
+
OAuth.popup(provider)
116
+
.done(function(result) {
117
+
result.post('/message', {
118
+
data: {
119
+
user_id:93,
120
+
content:'Hello Mr. 93 !'
121
+
}
122
+
})
123
+
.done(function (response) {
124
+
//this will display the id of the message in the console
125
+
console.log(response.id);
126
+
})
127
+
.fail(function (err) {
128
+
//handle error with err
129
+
});
130
+
})
131
+
.fail(function (err) {
132
+
//handle error with err
133
+
});
134
+
```
135
+
136
+
**PUT Request**
137
+
138
+
To make a PUT request, you have to call the `result.post` method like this :
139
+
140
+
```javascript
141
+
//Let's say the /profile endpoint on the provider waits for
142
+
//a PUT request to update the authenticated user's profile
143
+
//containing the field "name" and returns the field "name"
144
+
//containing the new name
145
+
OAuth.popup(provider)
146
+
.done(function(result) {
147
+
result.put('/message', {
148
+
data: {
149
+
name:"John Williams Doe III"
150
+
}
151
+
})
152
+
.done(function (response) {
153
+
//this will display the new name in the console
154
+
console.log(response.name);
155
+
})
156
+
.fail(function (err) {
157
+
//handle error with err
158
+
});
159
+
})
160
+
.fail(function (err) {
161
+
//handle error with err
162
+
});
163
+
```
164
+
165
+
**PATCH Request**
166
+
167
+
To make a PATCH request, you have to call the `result.patch` method like this :
168
+
169
+
```javascript
170
+
//Let's say the /profile endpoint on the provider waits for
171
+
//a PATCH request to update the authenticated user's profile
172
+
//containing the field "name" and returns the field "name"
173
+
//containing the new name
174
+
OAuth.popup(provider)
175
+
.done(function(result) {
176
+
result.patch('/message', {
177
+
data: {
178
+
name:"John Williams Doe III"
179
+
}
180
+
})
181
+
.done(function (response) {
182
+
//this will display the new name in the console
183
+
console.log(response.name);
184
+
})
185
+
.fail(function (err) {
186
+
//handle error with err
187
+
});
188
+
})
189
+
.fail(function (err) {
190
+
//handle error with err
191
+
});
192
+
```
193
+
194
+
**DELETE Request**
195
+
196
+
To make a DELETE request, you have to call the `result.del` method like this :
197
+
198
+
```javascript
199
+
//Let's say the /picture?id=picture_id endpoint on the provider waits for
200
+
//a DELETE request to delete a picture with the id "84"
201
+
//and returns true or false depending on the user's rights on the picture
202
+
OAuth.popup(provider)
203
+
.done(function(result) {
204
+
result.del('/picture?id=84')
205
+
.done(function (response) {
206
+
//this will display true if the user was authorized to delete
207
+
//the picture
208
+
console.log(response);
209
+
})
210
+
.fail(function (err) {
211
+
//handle error with err
212
+
});
213
+
})
214
+
.fail(function (err) {
215
+
//handle error with err
216
+
});
217
+
```
218
+
219
+
**Me() Request**
220
+
221
+
The `me()` request is an OAuth.io feature that allows you, when the provider is supported, to retrieve a unified object describing the authenticated user. That can be very useful when you need to login a user via several providers, but don't want to handle a different response each time.
222
+
223
+
To use the `me()` feature, do like the following (the example works for Facebook, Github, Twitter and many other providers in this case) :
224
+
225
+
```javascript
226
+
//provider can be 'facebook', 'twitter', 'github', or any supported
227
+
//provider that contain the fields 'firstname' and 'lastname'
228
+
//or an equivalent (e.g. "FirstName" or "first-name")
229
+
var provider ='facebook';
230
+
231
+
OAuth.popup(provider)
232
+
.done(function(result) {
233
+
result.me()
234
+
.done(function (response) {
235
+
console.log('Firstname: ', response.firstname);
236
+
console.log('Lastname: ', response.lastname);
237
+
})
238
+
.fail(function (err) {
239
+
//handle error with err
240
+
});
241
+
})
242
+
.fail(function (err) {
243
+
//handle error with err
244
+
});
245
+
```
246
+
247
+
*Filtering the results*
248
+
249
+
You can filter the results of the `me()` method by passing an array of fields you need :
0 commit comments