Last active
December 18, 2015 04:39
-
-
Save chengyuhui/5727054 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
request = require 'request' | |
_ = require 'underscore' | |
{E} = require './error' | |
{make_esc} = require 'iced-error' | |
client_id = '545553600905.apps.googleusercontent.com' | |
client_secret = 'q-tA1vMJhkoeoucVH7NfvryX' | |
red_uri = 'urn:ietf:wg:oauth:2.0:oob' | |
endpoint = 'https://www.googleapis.com/rpc?prettyPrint=false' | |
appVersion = 16077 | |
refresh = (code,gcb)-> | |
esc = make_esc gcb | |
option={ | |
url:'https://accounts.google.com/o/oauth2/token' | |
method:'POST' | |
encoding:'utf-8' | |
headers:{ | |
'User-Agent':'gtm-oauth2 com.google.GooglePlus/4.2.0' | |
'Content-Type':'application/x-www-form-urlencoded' | |
'Accept-Language':'zh-cn' | |
} | |
#proxy:'http://127.0.0.1:8888' | |
form:{ | |
client_id | |
client_secret | |
grant_type:'refresh_token' | |
refresh_token:code | |
} | |
} | |
await request option,esc defer(rep,body) | |
try | |
body = JSON.parse body | |
catch e | |
return gcb(e) | |
return gcb new Error(body.error) if body.error? | |
return gcb null,"#{body.token_type} #{body.access_token}" | |
### | |
The main API class | |
@mixin | |
### | |
class GPlusAPI | |
### | |
Create an new API object | |
@param refreshToken [String] The refresh token is used to login to the API and refresh the access token. | |
@param userID [String] The user's Google+ profile ID. **Don't pass Integer** | |
### | |
constructor:(@refreshToken,@userID)-> | |
### | |
Make API request. | |
You can call this function manually if you want to make custom request. | |
@param method [String] The JSON-RPC object's 'method' property. | |
@param param [Object] The method's parameter. | |
@param callback [function(err,result)] Called when the request executed. | |
### | |
makeRequest:(method,param,callback)=> | |
esc = make_esc callback | |
body ={ | |
id:Math.round Math.random()*(233-1)+1 | |
jsonrpc:'2.0' | |
apiVersion:'v2' | |
method | |
params:{} | |
} | |
body.params = _.extend param,{ | |
commonFields:{ | |
appVersion | |
effectiveUser:@userID | |
sourceInfo:'native:iphone_app' | |
} | |
} | |
body = JSON.stringify body | |
if not @access? | |
await refresh @refreshToken,esc defer(@access) | |
options = { | |
url:endpoint | |
method:'POST' | |
encoding:'utf-8' | |
body | |
headers:{ | |
'Accept':'application/json-rpc', | |
'Accept-Language':'zh-cn', | |
'User-Agent':'Mozilla/5.0 (iPad1,1; U; CPU iPhone OS 5_1_1 like Mac OS X; zh_CN) com.google.GooglePlus/13196 (KHTML, like Gecko) Mobile/K48AP (gzip)', | |
'Authorization':@access | |
} | |
} | |
await request options,esc defer(res,body) | |
if body.error?.message is 'Invalid Credentials' | |
await refresh @refreshToken,esc defer(@access) | |
options = { | |
url:endpoint | |
method:'POST' | |
encoding:'utf-8' | |
body | |
headers:{ | |
'Accept':'application/json-rpc', | |
'Accept-Language':'zh-cn', | |
'User-Agent':'Mozilla/5.0 (iPad1,1; U; CPU iPhone OS 5_1_1 like Mac OS X; zh_CN) com.google.GooglePlus/13196 (KHTML, like Gecko) Mobile/K48AP (gzip)', | |
'Authorization':@access | |
} | |
} | |
await request options,esc defer(res,body) | |
try | |
body = JSON.parse body | |
catch e | |
return callback new Error E.msg.INVALID_JSON | |
if body.error? | |
return callback(new Error body.error.message) | |
return callback null,body.result | |
### | |
Get the current user's mobile app settings(including multi-account infomation) | |
@param callback [function(err,result)] Called when the *plusi.ozinternal.getmobilesettings* method executed | |
### | |
getMobileSettings:(callback)=>@makeRequest 'plusi.ozinternal.getmobilesettings',{},callback | |
module.exports = GPlusAPI |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generated by IcedCoffeeScript 1.6.2d | |
(function() { | |
var E, GPlusAPI, appVersion, client_id, client_secret, endpoint, iced, make_esc, red_uri, refresh, request, _, __iced_k, __iced_k_noop, | |
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; | |
iced = require('iced-coffee-script').iced; | |
__iced_k = __iced_k_noop = function() {}; | |
request = require('request'); | |
_ = require('underscore'); | |
E = require('./error').E; | |
make_esc = require('iced-error').make_esc; | |
client_id = '545553600905.apps.googleusercontent.com'; | |
client_secret = 'q-tA1vMJhkoeoucVH7NfvryX'; | |
red_uri = 'urn:ietf:wg:oauth:2.0:oob'; | |
endpoint = 'https://www.googleapis.com/rpc?prettyPrint=false'; | |
appVersion = 16077; | |
refresh = function(code, gcb) { | |
var body, e, esc, option, rep, ___iced_passed_deferral, __iced_deferrals, __iced_k, | |
_this = this; | |
__iced_k = __iced_k_noop; | |
___iced_passed_deferral = iced.findDeferral(arguments); | |
esc = make_esc(gcb); | |
option = { | |
url: 'https://accounts.google.com/o/oauth2/token', | |
method: 'POST', | |
encoding: 'utf-8', | |
headers: { | |
'User-Agent': 'gtm-oauth2 com.google.GooglePlus/4.2.0', | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Accept-Language': 'zh-cn' | |
}, | |
form: { | |
client_id: client_id, | |
client_secret: client_secret, | |
grant_type: 'refresh_token', | |
refresh_token: code | |
} | |
}; | |
(function(__iced_k) { | |
__iced_deferrals = new iced.Deferrals(__iced_k, { | |
parent: ___iced_passed_deferral, | |
filename: "api.coffee", | |
funcname: "refresh" | |
}); | |
request(option, esc(__iced_deferrals.defer({ | |
assign_fn: (function() { | |
return function() { | |
rep = arguments[0]; | |
return body = arguments[1]; | |
}; | |
})(), | |
lineno: 32 | |
}))); | |
__iced_deferrals._fulfill(); | |
})(function() { | |
try { | |
body = JSON.parse(body); | |
} catch (_error) { | |
e = _error; | |
return gcb(e); | |
} | |
if (body.error != null) { | |
return gcb(new Error(body.error)); | |
} | |
return gcb(null, "" + body.token_type + " " + body.access_token); | |
}); | |
}; | |
/* | |
The main API class | |
@mixin | |
*/ | |
GPlusAPI = (function() { | |
/* | |
Create an new API object | |
@param refreshToken [String] The refresh token is used to login to the API and refresh the access token. | |
@param userID [String] The user's Google+ profile ID. **Don't pass Integer** | |
*/ | |
function GPlusAPI(refreshToken, userID) { | |
this.refreshToken = refreshToken; | |
this.userID = userID; | |
this.getMobileSettings = __bind(this.getMobileSettings, this); | |
this.makeRequest = __bind(this.makeRequest, this); | |
} | |
/* | |
Make API request. | |
You can call this function manually if you want to make custom request. | |
@param method [String] The JSON-RPC object's 'method' property. | |
@param param [Object] The method's parameter. | |
@param callback [function(err,result)] Called when the request executed. | |
*/ | |
GPlusAPI.prototype.makeRequest = function(method, param, callback) { | |
var body, e, esc, options, res, ___iced_passed_deferral, __iced_deferrals, __iced_k, | |
_this = this; | |
__iced_k = __iced_k_noop; | |
___iced_passed_deferral = iced.findDeferral(arguments); | |
esc = make_esc(callback); | |
body = { | |
id: Math.round(Math.random() * (233 - 1) + 1), | |
jsonrpc: '2.0', | |
apiVersion: 'v2', | |
method: method, | |
params: {} | |
}; | |
body.params = _.extend(param, { | |
commonFields: { | |
appVersion: appVersion, | |
effectiveUser: this.userID, | |
sourceInfo: 'native:iphone_app' | |
} | |
}); | |
body = JSON.stringify(body); | |
(function(__iced_k) { | |
if (_this.access == null) { | |
(function(__iced_k) { | |
__iced_deferrals = new iced.Deferrals(__iced_k, { | |
parent: ___iced_passed_deferral, | |
filename: "api.coffee", | |
funcname: "GPlusAPI.makeRequest" | |
}); | |
refresh(_this.refreshToken, esc(__iced_deferrals.defer({ | |
assign_fn: (function(__slot_1) { | |
return function() { | |
return __slot_1.access = arguments[0]; | |
}; | |
})(_this), | |
lineno: 87 | |
}))); | |
__iced_deferrals._fulfill(); | |
})(__iced_k); | |
} else { | |
return __iced_k(); | |
} | |
})(function() { | |
options = { | |
url: endpoint, | |
method: 'POST', | |
encoding: 'utf-8', | |
body: body, | |
headers: { | |
'Accept': 'application/json-rpc', | |
'Accept-Language': 'zh-cn', | |
'User-Agent': 'Mozilla/5.0 (iPad1,1; U; CPU iPhone OS 5_1_1 like Mac OS X; zh_CN) com.google.GooglePlus/13196 (KHTML, like Gecko) Mobile/K48AP (gzip)', | |
'Authorization': _this.access | |
} | |
}; | |
(function(__iced_k) { | |
__iced_deferrals = new iced.Deferrals(__iced_k, { | |
parent: ___iced_passed_deferral, | |
filename: "api.coffee", | |
funcname: "GPlusAPI.makeRequest" | |
}); | |
request(options, esc(__iced_deferrals.defer({ | |
assign_fn: (function() { | |
return function() { | |
res = arguments[0]; | |
return body = arguments[1]; | |
}; | |
})(), | |
lineno: 103 | |
}))); | |
__iced_deferrals._fulfill(); | |
})(function() { | |
(function(__iced_k) { | |
var _ref; | |
if (((_ref = body.error) != null ? _ref.message : void 0) === 'Invalid Credentials') { | |
(function(__iced_k) { | |
__iced_deferrals = new iced.Deferrals(__iced_k, { | |
parent: ___iced_passed_deferral, | |
filename: "api.coffee", | |
funcname: "GPlusAPI.makeRequest" | |
}); | |
refresh(_this.refreshToken, esc(__iced_deferrals.defer({ | |
assign_fn: (function(__slot_1) { | |
return function() { | |
return __slot_1.access = arguments[0]; | |
}; | |
})(_this), | |
lineno: 106 | |
}))); | |
__iced_deferrals._fulfill(); | |
})(function() { | |
options = { | |
url: endpoint, | |
method: 'POST', | |
encoding: 'utf-8', | |
body: body, | |
headers: { | |
'Accept': 'application/json-rpc', | |
'Accept-Language': 'zh-cn', | |
'User-Agent': 'Mozilla/5.0 (iPad1,1; U; CPU iPhone OS 5_1_1 like Mac OS X; zh_CN) com.google.GooglePlus/13196 (KHTML, like Gecko) Mobile/K48AP (gzip)', | |
'Authorization': _this.access | |
} | |
}; | |
(function(__iced_k) { | |
__iced_deferrals = new iced.Deferrals(__iced_k, { | |
parent: ___iced_passed_deferral, | |
filename: "api.coffee", | |
funcname: "GPlusAPI.makeRequest" | |
}); | |
request(options, esc(__iced_deferrals.defer({ | |
assign_fn: (function() { | |
return function() { | |
res = arguments[0]; | |
return body = arguments[1]; | |
}; | |
})(), | |
lineno: 119 | |
}))); | |
__iced_deferrals._fulfill(); | |
})(__iced_k); | |
}); | |
} else { | |
return __iced_k(); | |
} | |
})(function() { | |
try { | |
body = JSON.parse(body); | |
} catch (_error) { | |
e = _error; | |
return callback(new Error(E.msg.INVALID_JSON)); | |
} | |
if (body.error != null) { | |
return callback(new Error(body.error.message)); | |
} | |
return callback(null, body.result); | |
}); | |
}); | |
}); | |
}; | |
/* | |
Get the current user's mobile app settings(including multi-account infomation) | |
@param callback [function(err,result)] Called when the *plusi.ozinternal.getmobilesettings* method executed | |
*/ | |
GPlusAPI.prototype.getMobileSettings = function(callback) { | |
return this.makeRequest('plusi.ozinternal.getmobilesettings', {}, callback); | |
}; | |
return GPlusAPI; | |
})(); | |
module.exports = GPlusAPI; | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment