Skip to content
This repository was archived by the owner on Jun 11, 2025. It is now read-only.

Commit 498f1df

Browse files
author
Eric Koleda
committed
Added Xing sample and updated library to correctly handle 201 responses.
1 parent bc3e6df commit 498f1df

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

Service.gs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ Service_.prototype.getRequestToken_ = function() {
326326
}
327327

328328
var response = this.fetchInternal_(url, params, null, oauthParams);
329-
if (response.getResponseCode() != 200) {
329+
if (response.getResponseCode() >= 400) {
330330
throw 'Error starting OAuth flow: ' + response.getContentText();
331331
}
332332

@@ -359,7 +359,7 @@ Service_.prototype.getAccessToken_ = function(opt_verifier) {
359359
}
360360

361361
var response = this.fetchInternal_(url, params, token, oauthParams);
362-
if (response.getResponseCode() != 200) {
362+
if (response.getResponseCode() >= 400) {
363363
throw 'Error completing OAuth flow: ' + response.getContentText();
364364
}
365365

samples/Xing.gs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
var CONSUMER_KEY = '...';
2+
var CONSUMER_SECRET = '...';
3+
4+
/**
5+
* Authorizes and makes a request to the Xing API.
6+
*/
7+
function run() {
8+
var service = getService();
9+
if (service.hasAccess()) {
10+
var url = 'https://api.xing.com/v1/users/me';
11+
var response = service.fetch(url);
12+
var result = JSON.parse(response.getContentText());
13+
Logger.log(JSON.stringify(result, null, 2));
14+
} else {
15+
var authorizationUrl = service.authorize();
16+
Logger.log('Open the following URL and re-run the script: %s',
17+
authorizationUrl);
18+
}
19+
}
20+
21+
/**
22+
* Reset the authorization state, so that it can be re-tested.
23+
*/
24+
function reset() {
25+
var service = getService();
26+
service.reset();
27+
}
28+
29+
/**
30+
* Configures the service.
31+
*/
32+
function getService() {
33+
return OAuth1.createService('Xing')
34+
// Set the endpoint URLs.
35+
.setRequestTokenUrl('https://api.xing.com/v1/request_token')
36+
.setAuthorizationUrl('https://api.xing.com/v1/authorize')
37+
.setAccessTokenUrl('https://api.xing.com/v1/access_token')
38+
39+
// Set the consumer key and secret.
40+
.setConsumerKey(CONSUMER_KEY)
41+
.setConsumerSecret(CONSUMER_SECRET)
42+
43+
// Set the name of the callback function in the script referenced
44+
// above that should be invoked to complete the OAuth flow.
45+
.setCallbackFunction('authCallback')
46+
47+
// Set the property store where authorized tokens should be persisted.
48+
.setPropertyStore(PropertiesService.getUserProperties());
49+
}
50+
51+
/**
52+
* Handles the OAuth2 callback.
53+
*/
54+
function authCallback(request) {
55+
var service = getService();
56+
var authorized = service.handleCallback(request);
57+
if (authorized) {
58+
return HtmlService.createHtmlOutput('Success!');
59+
} else {
60+
return HtmlService.createHtmlOutput('Denied');
61+
}
62+
}

0 commit comments

Comments
 (0)