无法从OAuth Google API获取已安装应用程序的访问令牌

时间:2013-09-04 16:46:44

标签: javascript google-api oauth-2.0 google-oauth extendscript

我正在创建OAuth身份验证流程,因此我安装的应用程序的用户可以访问其私有Google电子表格文档。我使用Adobe ExtendScript进行编码,因此我无法使用Google提供的Javascript客户端库。我已经阅读了Google's OAuth 2.0 documentation for installed applications了很多次,但需要有关OAuth流程的一个方面的帮助。我可以通过从已安装的应用程序启动浏览器并让用户提交其凭据,然后将授权代码复制并粘贴回应用程序来获取授权代码。但是,当我POST到端点以交换访问令牌的授权代码时,它不起作用。来自谷歌的回复主体显示了这一点:

Moved Temporarily
The document has moved <A HREF="https://accounts.google.com/o/oauth2/token">here</A>.

然而,我对href标签中找到的完全相同的URL进行了POST调用。所以,我不确定为什么谷歌会告诉我,当我发布到同一个网址时,终点已暂时移动。这是我用来生成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);

}

帖子很好,但有人知道为什么这个'暂时移动'通知会出现在Google的回复中吗?任何建议都非常感谢!

编辑:只是为了澄清,这是我的脚本在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&

如果我使用cURL中的授权代码以及其他参数,我会从Google的OAuth服务器获得成功的回复。所以,显然我的套接字与Google端点的交互方式有所不同,但我不确定是什么。某些组件是否可能需要进行URI编码,而其他组件则不可能?这是我正在使用的cURL:

#!/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

编辑2:所以,由于套接字在Extendscript中不支持SSL,我编写了一个使用OS级别调用来调用请求的函数。如果在OSX上,我们可以假设我们可以访问cURL,但在Windows上,我们必须编写一个VBScript,它通过命令行中的cscript主机执行。对于ExtendScript,这是发出Web请求的函数:

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 to make a `"+ method +"` request to the 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;
}

这是从ExtendScript库调用的VBScript脚本。它需要三个参数,所有字符串:

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" Then
        oHTTP.open "POST", sUrl,false
    ElseIf sMethod = "GET" Then
        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 

您可以在ExtendScript中将此用于任何API端点,但响应将始终为字符串。因此,对于Google的OAuth端点,您将获得一个看起来像JSON的字符串。所以,你必须用JSON.parse()等解析它。

1 个答案:

答案 0 :(得分:3)

请参阅上面的编辑以获得答案。基本上它归结为ExtendScript中不支持SSL的Socket对象。上面的解决方案通过使用ExtendScript system.callSystem()方法获取OSX上的cURL和Windows上的VBScript来显示解决方法。