尝试使用需要身份验证的 API 使用 Google Script 从 TDAmeritrade 中提取股票报价信息 [英] Trying to pull stock quote info from TDAmeritrade with Google Script with API needing authentication

查看:33
本文介绍了尝试使用需要身份验证的 API 使用 Google Script 从 TDAmeritrade 中提取股票报价信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 TD Ameritrade 的 API 来提取股票行情信息.该脚本需要身份验证才能提取实时数据.CURL命令如下:

I'm using an API from TD Ameritrade to pull stock ticker information. The script needs authentication to pull real time data. The CURL command is as follows:

curl -X GET --header "Authorization:" --header "Authorization: Bearer" "https://api.tdameritrade.com/v1/marketdata/AAPL/quotes?apikey="

curl -X GET --header "Authorization: " --header "Authorization: Bearer " "https://api.tdameritrade.com/v1/marketdata/AAPL/quotes?apikey="

我正在使用 Google 脚本从 API 读取数据,进行一些计算,然后将数据写入 Google 表格.下面是验证的脚本,它不起作用.请注意,此处未显示我的访问令牌和 API 密钥,文本以 和 标注.该脚本不会给出错误消息,并会提取(延迟)数据.我可以判断身份验证没有正常工作,因为数据被延迟了.在 TD Ameritrade 站点上通过身份验证进行测试会返回实时数据.在我的 PC 上的 DOS 框中运行 CURL 也是如此.

I'm using a Google Script to read the data from the API, do some calculations, and then write the data to a Google Sheet. Below is the script to authenticate, and it doesn't work. Note that my access token and APIkeys are not displayed here and the text is noted by and . The script does not give an error message, and pulls (delayed) data. I can tell the authentication isn't working right, because the data is delayed. Testing on the TD Ameritrade site with authentication returns real time data. So does running the CURL in a DOS box on my PC.

var ticker = "AAPL"
var options = {
    "method" : "GET",
    "Authorization" : "Bearer <access token>"
     }
var calltoAPI = UrlFetchApp.fetch("https://api.tdameritrade.com/v1/marketdata/"+ticker+"/quotes?apikey=<APIkey>",options);

我对此很陌生,希望得到任何帮助

I'm fairly new at this and would appreciate any help

更新:这是有效的代码

    var ticker = "APPL"

    var headers = {"Authorization":" Bearer <access token>"}
    var options = {"headers":headers}

    var calltoAPI=urlFetchApp.fetch("https://api.tdameritrade.com/v1/marketdata/"+ticker+"/quotes?apikey=<apikey>",options);

推荐答案

  • 您想将以下 curl 命令转换为 Google Apps 脚本.
    • curl -X GET --header "授权:" --header "授权:不记名" "https://api.tdameritrade.com/v1/marketdata/AAPL/quotes?apikey="
    • 您已经确认此 curl 命令可以正常工作.
    • 如果我的理解是正确的,这个答案怎么样?当 --header "Authorization: Bearer " 用于 Google Apps Script 时,请将其放入 header 对象.所以请修改如下.

      If my understanding is correct, how about this answer? When --header "Authorization: Bearer " is used for Google Apps Script, please put it to the header object. So please modify as follows.

      var options = {
          "method" : "GET",
          "Authorization" : "Bearer <access token>"
           }
      

      致:

      var options = {
        "method" : "GET",
        "headers": {"Authorization" : "Bearer <access token>"}
      };
      

      参考:

      • fetch(url,参数)
      • 您的 curl 命令和修改后的脚本的请求是相同的.但如果这不起作用,请确认令牌.

        The requests of both your curl command and the modified script are the same. But if this didn't work, please confirm the tokens.

        这篇关于尝试使用需要身份验证的 API 使用 Google Script 从 TDAmeritrade 中提取股票报价信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆