ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-11-22 21:37:38  阅读:282  来源: 互联网

标签:extendscript oauth-2-0 google-api google-oauth javascript


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

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

但是,我对在href标签中找到的完全相同的URL进行了POST调用.因此,我不确定为什么google会告诉我,当我发布到相同的URL时,端点已经临时移动了.这是我用来生成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的回复中会出现“临时移动”通知吗?任何建议,不胜感激!

编辑:只是为了澄清,这是我的脚本在原始文件中提出的请求:

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:因此,由于Sockets在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()之类的内容对其进行解析.

解决方法:

有关此问题的答案,请参见上面的编辑.从本质上讲,它可以归结为ExtendScript中不支持SSL的Socket对象.上面的解决方案通过使用ExtendScript system.callSystem()方法在OSX上获取cURL并在Windows上获取VBScript展示了一种解决方法.

标签:extendscript,oauth-2-0,google-api,google-oauth,javascript
来源: https://codeday.me/bug/20191122/2062706.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有