尝试使用需要身份验证的 API 使用 Google Script 从 TDAmeritrade 中提取股票报价信息

编程入门 行业动态 更新时间:2024-10-28 14:27:19
本文介绍了尝试使用需要身份验证的 API 使用 Google Script 从 TDAmeritrade 中提取股票报价信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在使用 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/v1/marketdata/AAPL/quotes?apikey="

curl -X GET --header "Authorization: " --header "Authorization: Bearer " "https://api.tdameritrade/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/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/v1/marketdata/"+ticker+"/quotes?apikey=<apikey>",options);

推荐答案

您想将以下 curl 命令转换为 Google Apps 脚本.curl -X GET --header "授权:" --header "授权:不记名" "https://api.tdameritrade/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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-18 09:49:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/935706.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:身份验证   股票   信息   API   Script

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!