This repository has been archived by the owner on May 25, 2020. It is now read-only.
forked from apaleslimghost/accounts-pinterest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pinterest_server.js
92 lines (75 loc) · 2.49 KB
/
pinterest_server.js
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
Pinterest = {};
var querystring = Npm.require('querystring');
OAuth.registerService('pinterest', 2, null, function(query) {
var expiresAt = new Date()
expiresAt.setFullYear(expiresAt.getFullYear() + 1);
// console.log('query', query);
var response = getTokenResponse(query);
var accessToken = response.accessToken;
var whitelisted = ['id', 'first_name', 'last_name', 'scope'];
var identity = getIdentity(accessToken, whitelisted);
var serviceData = _.extend({
accessToken: accessToken
}, identity.data);
// console.log('identity', identity);
// console.log('serviceData', serviceData);
return {
serviceData: serviceData,
options: { profile: {name: identity.data.first_name + ' ' + identity.data.last_name} }
};
});
// returns an object containing:
// - accessToken
// - expiresIn: lifetime of token in seconds
var getTokenResponse = function (query) {
var config = ServiceConfiguration.configurations.findOne({service: 'pinterest'});
if (!config)
throw new ServiceConfiguration.ConfigError();
var responseContent;
try {
// Request an access token
responseContent = HTTP.post(
"https://api.pinterest.com/v1/oauth/token",
{
headers: {"User-Agent": "Meteor/1.0"},
params: {
grant_type: 'authorization_code',
client_id: config.clientId,
redirect_uri: OAuth._redirectUri('pinterest', config),
client_secret: OAuth.openSecret(config.secret),
code: query.code
}
}
);
} catch (err) {
throw _.extend(new Error("Failed to complete OAuth handshake with Pinterest. " + err.message),
{response: err.response});
}
// console.log('responseContent', responseContent);
// Success! Extract the pinterest access token and expiration
// time from the response
var accessToken = responseContent.data.access_token;
if (!accessToken) {
throw new Error("Failed to complete OAuth handshake with Pinterest " +
"-- can't find access token in HTTP response. " + responseContent);
}
return {
accessToken: accessToken
};
};
var getIdentity = function (accessToken, fields) {
try {
// console.log('accessToken request GET', accessToken);
return HTTP.get("https://api.pinterest.com/v1/me", {
params: {
access_token: accessToken,
}
}).data;
} catch (err) {
throw _.extend(new Error("Failed to fetch identity from Pinterest. " + err.message),
{response: err.response});
}
};
Pinterest.retrieveCredential = function(credentialToken, credentialSecret) {
return OAuth.retrieveCredential(credentialToken, credentialSecret);
};