首页 > 解决方案 > 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.

标签: pythonurllib2urlliburllib3datadog

解决方案


在搜索其他问题时,我发现解决此问题所需要做的就是在 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

推荐阅读