blob: 68cb0911aff6d2629d3bf1fb08e926d5e945454e [file] [log] [blame]
avm9996304def3e2016-11-27 22:53:05 +01001const FB_APP_ID = CURRENT_LOCALE == 'zh_CN' ? 170328509685996 : 118170701590738;
2const FB_REDIRECT_URI = 'http://www.facebook.com/connect/login_success.html';
3const FB_PERMISSION = 'offline_access,user_photos,publish_stream';
4const FB_ACCESS_TOKEN_URL = 'https://www.facebook.com/dialog/oauth';
5const FB_PHOTO_UPLOAD_URL = 'https://graph.facebook.com/me/photos';
6const FB_USER_INFO_URL = 'https://graph.facebook.com/me';
7const FB_LOGOUT_URL = 'http://m.facebook.com/logout.php?confirm=1';
8
9var Facebook = {
10 siteId: 'facebook',
11 redirectUrl: FB_REDIRECT_URI,
12 currentUserId: null,
13 accessTokenCallback: null,
14
15 isRedirectUrl: function(url) {
16 return url.indexOf(FB_REDIRECT_URI) == 0;
17 },
18
19 getAccessToken: function(callback) {
20 Facebook.accessTokenCallback = callback;
21 var url = FB_ACCESS_TOKEN_URL + '?client_id=' + FB_APP_ID +
22 '&redirect_uri=' + FB_REDIRECT_URI + '&scope=' + FB_PERMISSION +
23 '&response_type=token';
24 chrome.tabs.create({url: url});
25 },
26
27 parseAccessToken: function(url) {
28 var queryString = url.split('#')[1] || url.split('?')[1];
29 var queries = queryString.split('&');
30 var queryMap = {};
31 queries.forEach(function(pair) {
32 queryMap[pair.split('=')[0]] = pair.split('=')[1];
33 });
34 var accessToken = queryMap['access_token'];
35 if (accessToken) {
36 var user = new User({
37 accessToken: accessToken
38 });
39 Facebook.accessTokenCallback('success', user);
40 } else if (queryMap['error']) {
41 Facebook.accessTokenCallback('failure', 'user_denied');
42 }
43 Facebook.accessTokenCallback = null;
44 },
45
46 getUserInfo: function(user, callback) {
47 ajax({
48 url: FB_USER_INFO_URL,
49 parameters: {
50 'access_token': user.accessToken
51 },
52 success: function(userInfo) {
53 userInfo = JSON.parse(userInfo);
54 if (callback) {
55 user.id = userInfo.id;
56 user.name = userInfo.name;
57 callback('success', user);
58 }
59 },
60 status: {
61 others: function() {
62 if (callback)
63 callback('failure', 'failed_to_get_user_info');
64 }
65 }
66 });
67 },
68
69 upload: function(user, caption, imageData, callback) {
70 caption = ajax.encodeForBinary(caption);
71 var params = {
72 'access_token': user.accessToken,
73 message: caption
74 };
75
76 var binaryData = {
77 boundary: MULTIPART_FORMDATA_BOUNDARY,
78 data: imageData,
79 value: 'test.png',
80 type: 'image/png'
81 };
82
83 ajax({
84 url: FB_PHOTO_UPLOAD_URL,
85 parameters: params,
86 multipartData: binaryData,
87 success: function(data) {
88 callback('success', JSON.parse(data).id);
89 },
90 status: {
91 others: function(data) {
92 var message;
93 if (data) {
94 data = JSON.parse(data);
95 if (data.error.message.indexOf('access token') >= 0) {
96 // User removed application permission
97 // {"error":{"type":"OAuthException",
98 // "message":"Error validating access token."}}
99 message = 'bad_access_token';
100 } else {
101 // {"error":{"type":"OAuthException",
102 // "message":"(#1) An unknown error occurred"}}
103 message = 'unknown_error';
104 }
105 } else {
106 message = 'failed_to_connect_to_server';
107 }
108 callback('failure', message);
109 }
110 }
111 });
112 },
113
114 getPhotoLink: function(user, photoId, callback) {
115 ajax({
116 url: 'https://graph.facebook.com/' + photoId,
117 parameters: {
118 'access_token': user.accessToken
119 },
120 complete: function(statusCode, data) {
121 if (statusCode == 200) {
122 callback('success', JSON.parse(data).link);
123 } else {
124 callback('failure', 'failed_to_get_photo_link');
125 }
126 }
127 });
128 },
129
130 logout: function(callback) {
131 ajax({
132 url: FB_LOGOUT_URL,
133 success: function(data) {
134 if (callback)
135 callback(data);
136 }
137 });
138 }
139};