-
Notifications
You must be signed in to change notification settings - Fork 8.5k
/
Copy pathgapi-chrome-apps.js
138 lines (121 loc) · 4.16 KB
/
gapi-chrome-apps.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* Copyright 2013 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/**
* gapi-chrome-apps version 0.001
*
* Provides the Google API javascript client 'gapi' as
* appropriate for hosted websites, or if in a Chrome packaged
* app implement a minimal set of functionality that is Content
* Security Policy compliant and uses the chrome identity api.
*
* https://github.com/GoogleChrome/chrome-app-samples/tree/master/gapi-chrome-apps-lib
*
*/
"use strict";
(function () {
if (typeof gapi !== 'undefined')
throw new Error('gapi already defined.');
if (typeof gapiIsLoaded !== 'function')
throw new Error('gapiIsLoaded callback function must be defined prior to ' +
'loading gapi-chrome-apps.js');
// If not running in a chrome packaged app, load web gapi:
if (!(chrome && chrome.app && chrome.app.runtime)) {
// Load web gapi.
var script = document.createElement('script');
script.src = 'https://apis.google.com/js/client.js?onload=gapiIsLoaded';
document.documentElement.appendChild(script);
return;
}
window.gapi = {};
window.gapi.auth = {};
window.gapi.client = {};
var access_token = undefined;
gapi.auth.authorize = function (params, callback) {
if (typeof callback !== 'function')
throw new Error('callback required');
var details = {};
details.interactive = params.immediate === false || false;
if (params.accountHint) {
// Specifying this prevents the account chooser from appearing on Android.
details.accountHint = params.accountHint;
}
console.assert(!params.response_type || params.response_type == 'token');
var callbackWrapper = function (getAuthTokenCallbackParam) {
access_token = getAuthTokenCallbackParam;
// TODO: error conditions?
if (typeof access_token !== 'undefined')
callback({ access_token: access_token});
else
callback();
}
chrome.identity.getAuthToken(details, callbackWrapper);
};
gapi.client.request = function (args) {
if (typeof args !== 'object')
throw new Error('args required');
if (typeof args.callback !== 'function')
throw new Error('callback required');
if (typeof args.path !== 'string')
throw new Error('path required');
if (args.root && args.root === 'string') {
var path = args.root + args.path;
} else {
var path = 'https://www.googleapis.com' + args.path;
}
if (typeof args.params === 'object') {
var deliminator = '?';
for (var i in args.params) {
path += deliminator + encodeURIComponent(i) + "="
+ encodeURIComponent(args.params[i]);
deliminator = '&';
}
}
var xhr = new XMLHttpRequest();
xhr.open(args.method || 'GET', path);
xhr.setRequestHeader('Authorization', 'Bearer ' + access_token);
if (typeof args.body !== 'undefined') {
xhr.setRequestHeader('content-type', 'application/json');
xhr.send(JSON.stringify(args.body));
} else {
xhr.send();
}
xhr.onerror = function () {
// TODO, error handling.
debugger;
};
xhr.onload = function() {
var rawResponseObject = {
// TODO: body, headers.
gapiRequest: {
data: {
status: this.status,
statusText: this.statusText
}
}
};
var rawResp = JSON.stringify(rawResponseObject);
if (this.response) {
var jsonResp = JSON.parse(this.response);
args.callback(jsonResp, rawResp);
} else {
args.callback(null, rawResp);
}
};
};
// Call client handler when gapi is ready.
setTimeout(function () { gapiIsLoaded(); }, 0);
})();