javascript - Cannot get access token from OAuth Google API for installed applications -
i'm creating oauth authentication flow users of installed application can access private google spreadsheet documents. i'm coding adobe extendscript, cannot use javascript client libraries google provides. i've read google's oauth 2.0 documentation installed applications quite few times, need 1 aspect of oauth flow. able authorization code launching browser installed app, , having user submit credentials, , copy , pasting authorization code app. however, when post endpoint exchanging authorization code access token, doesn't work. body of response google shows this:
moved temporarily document has moved <a href="https://accounts.google.com/o/oauth2/token">here</a>. and yet, made post call exact same url found in href tag. so, i'm not sure why google tell me end point has moved temporarily when posted same url. here code i'm using generate post.
function getaccesstoken(authcode) { var http = require('http'), oauthaccessendpoint = 'https://accounts.google.com/o/oauth2/token', oauthaccessparams = {}; oauthaccessparams['code'] = authcode; oauthaccessparams['client_id'] = '{my_client_id}'; oauthaccessparams['client_secret'] = '{my_client_secret}'; oauthaccessparams['redirect_uri'] = 'urn:ietf:wg:oauth:2.0:oob'; oauthaccessparams['grant_type'] = 'authorization_code'; var response = http.post(oauthaccessendpoint, oauthaccessparams); } the post made fine, know why 'moved temporarily' notice come in google's response? suggestions appreciated!
edit: clarify, here request script making in raw:
post /o/oauth2/token http/1.1 user-agent: adobe extendscript accept: */* connection: close host: accounts.google.com content-type: application/x-www-form-urlencoded content-length: 226 code={authcode}&client_id={my_client_id}&client_secret={my_client_secret}&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code& if use authorization code in curl, along other params, successful response google's oauth servers. so, way sockets interacting google's endpoints, i'm not sure what. possible components need uri encoded, while others not? here curl using:
#!/bin/bash authcode="$1" postcontent="code=$authcode&client_id={my_client_id}&client_secret={my_client_secret}&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code&" echo $postcontent curl -v --data $postcontent https://accounts.google.com/o/oauth2/token edit 2: so, since sockets not support ssl in extendscript, wrote function uses os level calls invoke request. if on osx, can assume have access curl, on windows, have write vbscript gets executed via cscript host in command line. extendscript, here function makes web request:
function webrequest(method, endpoint, query){ var response = null, wincurl = working_dir.fsname + '\\lib\\curl.vbs', curlcmd = ''; try { if ( os() == "win" ) { curlcmd = 'cscript "' + wincurl + '" /method:' + method + ' /url:' + endpoint + ' /query:' + query + ' //nologo'; } else { if (method === "post") { curlcmd = 'curl -s -d "' + query + '" ' + endpoint; } else if (method === "get") { curlcmd = 'curl -s -g -d "' + query + '" ' + endpoint; } } response = system.callsystem(curlcmd); } catch (err) { alert("error\nunable make `"+ method +"` request network endpoint. please try again."); } return response; } function os(){ var os = system.osname; if (!os.length) { os = $.os; } app_os = ( os.indexof("win") != -1 ) ? "win" : "mac"; return app_os; } and here vbscript script called extendscript library. takes 3 params, strings:
set namedargs = wscript.arguments.named smethod = namedargs.item("method") surl = namedargs.item("url") srequest = namedargs.item("query") httppost smethod, surl, srequest function httppost(smethod, surl, srequest) set ohttp = createobject("microsoft.xmlhttp") if smethod = "post" ohttp.open "post", surl,false elseif smethod = "get" ohttp.open "get", surl,false end if ohttp.setrequestheader "content-type", "application/x-www-form-urlencoded" ohttp.setrequestheader "content-length", len(srequest) ohttp.send srequest httppost = ohttp.responsetext wscript.echo httppost end function you can use in extendscript api endpoint, response string. so, in case of google's oauth endpoints, you'll string looks json. so, y ou'll have parse json.parse().
see edits above answer this. boiled down socket objects in extendscript not supporting ssl. solution above shows workaround using extendscript system.callsystem() method @ curl on osx , vbscript on windows.
Comments
Post a Comment