尝试使用需要身份验证的API从带有Google Script的TDAmeritrade中提取股票报价信息 [英] Trying to pull stock quote info from TDAmeritrade with Google Script with API needing authentication
问题描述
我正在使用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
更新:这是起作用的代码
UPDATE: HERE IS THE CODE THAT WORKS
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 "Authorization: " --header "Authorization: Bearer " "https://api.tdameritrade.com/v1/marketdata/AAPL/quotes?apikey="
- 您已经确认此curl命令可以正常工作.
- You want to convert the following curl command to Google Apps Script.
curl -X GET --header "Authorization: " --header "Authorization: Bearer " "https://api.tdameritrade.com/v1/marketdata/AAPL/quotes?apikey="
- You have already confirmed that this curl command works fine.
如果我的理解是正确的,那么这个答案如何?当
--header "Authorization: Bearer "
用于Google Apps脚本时,请将其放在标题对象中.因此,请进行如下修改.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,参数)
- fetch(url, params)
Reference:
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屋!
-