python - How do you send an API key to Datadog using urllib?
问题描述
I currently have a program that allows me to create and post dashboards to Datadog programmatically. Using the API functions here, I was successfully able to create, update, and remove dashboards as I please. However, now I'd like to extract the skeleton of existing dashboards that I have already created from Datadog to see what has been added or removed. To do this, I need to figure out how to send the API key along with a request. I have no problem getting the higher level information about the boards, but I'd like to go a step further.
This is what I get by calling api.ScreenBoard.get_all()
{
'screenboards': [{
'read_only': <boolean>,
'resource': <resource-link>,
'description': <description>,
'created': <date>,
'title': <text>,
'modified': <date>,
'created_by': { ''' <creator information> ''' },
'id': <table-id>
}]
}
Now, the end goal is simply to pull JSON from the "resource" link given from this command. I've tried to use urllib and urllib2 to merge that link with the host site (like https://www.foo.com/{resource-link}
), but I keep getting the following results:
<addinfourl at 0000000000 whose fp = <socket._fileobject object at 0x000000000>>
OR
{"errors": ["API key required"]}
The code that triggered this error is:
def getSkeleton(self):
boards = self.getAll(); # utilizing the api.ScreenBoards.get_all() function
boardList = boards['screenboards'];
for x in boardList:
url = self.target + x['resource']; # creating the JSON url
data = urllib.urlopen(url).read();
print data
As you can see, my "data" variable returns the error. So, all I need is to figure out how to send the API key along with my request to resolve the issue. If anyone knows how to perform this task, I would really appreciate it.
解决方案
在搜索其他问题时,我发现解决此问题所需要做的就是在 URL 中指定 API 密钥和应用程序密钥。考虑以下。
def getSkeleton(self):
api_key = 'your api key';
app_key = 'your application key';
boards = self.getAll(); # utilizing the api.ScreenBoards.get_all() function
boardList = boards['screenboards'];
for x in boardList:
url = self.target + x['resource'] + "?api_key=" + api_key +"&application_key=" + app_key;
data = urllib.urlopen(url).read();
print data
推荐阅读
- modelica - 如何解决与不在我的 OpenModelica 模型中的变量有关的错误?
- webpack - 使用 dev/hot-middleware 编译时如何忽略对 index.html 的更改?
- javascript - 无法将 javascript 添加到 chrome 扩展 html
- html - 如何在不影响顶部内容的情况下使背景图像模糊?
- javascript - 无法设置属性...未定义或空引用
- javascript - addeventlistener 后如何做到这一点不要覆盖 css 样式
- r - 如何添加这样的注释?
- sql - 如何从包含 oracle 模式中的列名的所有表中获取特定列的最大值?
- android - 在 ionic 3 中“找不到已安装的 Gradle 版本”
- cuda - 使用 Vulkan VkImage 作为 CUDA cuArray