Python发布api请求

时间:2018-02-12 00:21:35

标签: python

我需要在API中发帖子。我正在使用python。我是新手,我无法创建广告代码。我尝试用api示例信息创建一个dict,但它没有用。当我运行>>> sitios_creados时,答案就是'' 当我跑sites.status_code时,我会看到`415。 我不明白为什么,因为如果你在我的代码中看到我之前使用令牌做了正确的post python请求

我必须使用publisherid并使用它,并且令牌ID会创建广告代码。

我的发布者是:15663

我的代码:

import requests
import json

headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Authorization': 'Basic ',
}

data = [
  ('grant_type', 'password'),
  ('username', ''),
  ('password', ''),
]

response = requests.post('http://api.site.com/v1/oauth/generateOauthToken', headers=headers, data=data)
json_data = json.loads(response.text)
token = json_data['access_token'].encode("utf-8")


headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Authorization': 'Bearer {}'.format(token)
}


sites = requests.post('http://api.site.com/v1/inventorymgmt/publisherAdTag?entityId=15663', headers=headers, data=data)
sitios_creados = sites.content

Api信息示例:

URL: http://api.site.com/v1/inventorymgmt/publisherAdTag?entityId=2685
Method: POST
Request Body:
{
    "publisherId": 2685,
    "publisherSiteurl": "http://example.org",
    "adTagName": "THIS_IS_TEST_DEMAND_5",
    "adCodeTypeId": 1,
    "foldPlacementId": 1,
    "adTypeId": 3,
    "pagePlacementId": 1,
    "adExpansionDirectionId": 1,
    "adSize": {

        "name": null,
        "width": 0,
        "height": 0,
        "id": 9
    },
    "adTagPlacements": [{
        "adTagPlacementId": 0,
        "linkOnlyToGeo": false,
        "ecpm": 1,
        "adScript": "THIS IS DEMO SCRIPT",
        "currency": 1
    }],
    "adTagCustomParamMap": [{
        "name": "kadcarrier",
        "macroValue": "techno.carrier"
    }, {
        "name": "kadcity",
        "macroValue": "geo.city"
    }]
}

1 个答案:

答案 0 :(得分:0)

它是网站的API吗?

如果是,您可以在浏览器的开发人员工具中进行网络分析,并复制POST包的curl命令。

然后你浏览curl.trillworks.com并将curl命令转换为Python POST请求。

在您的python请求中,您可以修改值。