导出gooddata报告python

时间:2018-03-14 04:00:25

标签: python api gooddata

希望连接到Gooddata API并通过python中的API导出报告。 The documentation有点令人困惑。

我已经定义了对我的gooddata实例的登录:

from urllib2 import Request, urlopen
import json
import requests

def login_gooddata(my_email, my_password):
    url = 'https://secure.gooddata.com/gdc/account/login'    

    values = {
        "postUserLogin": {
            "login": my_email,
            "password": my_password,
            "remember": 0,
            "verify_level": 0
        }
    }

    headers = {
      'Content-Type': 'application/json',
      'Accept': 'application/json'
    }
    encoded_values = json.dumps(values)
    #request = Request(url, data=encoded_values, headers=headers)
    r = requests.post(url, data=encoded_values)    
    return r

成功登录,返回200响应。

鉴于gooddata网站上有关连接API的文档,我试图导出原始项目文件。

我设置了项目和对象ID:

project_id = 'asibfakuyebkbhdbfaisdf'
object_id = '87234760'


values = {
    "report_req": {
    "reportDefinition": "/gdc/md/"+ project_id + "/obj/" + object_id
    }
  }


headers = {
  'Accept': 'application/json',
  'Content-Type': 'application/json'
}

url = 'https://secure.gooddata.com/gdc/app/projects/' + project_id + '/execute/raw/'

r = requests.post(url, data=json.dumps(values), headers=headers)

request = Request(url, data=json.dumps(values), headers=headers)

response_body = urlopen(requests).read()
print response_body

我使用r = requests.post(url, data=encoded_valuesrequest = Request(url, data=encoded_values, headers=headers)。仍然收到错误。我真的不确定如何处理下一步。

遵循连接API的文档中所述的说明:

enter image description here

1 个答案:

答案 0 :(得分:1)

您需要执行来自单个"会话的所有HTTP请求"会记住登录中的Cookie:执行s = requests.Session()一次,然后使用s.post代替requests.post

有关详情,请参阅https://stackoverflow.com/a/31571805/3407728