全部产品
阿里云办公

Python-API开发实例

更新时间:2017-08-17 10:40:43

Python常用API调用样例

线下联系营销引擎产品小二,获取SDK开发包。

阅读阿里云SDK调用文档,安装想换环境:https://help.aliyun.com/document_detail/53090.html?spm=5176.doc53090.6.541.YaMxUL

开发之前,替换代码中变量:

accessId、accessKey:通过阿里云官网获取ak信息,https://ak-console.aliyun.com/?spm=5176.2020520207.1001.d605.6bab83ec2qx5rX#/accesskey)

advId:可以在自身RDS中看表’dsp_pt_advertiser_info’中adv_id字段。也可以在广告公司后台前端页面直接看:样例

创建计划:

  1. from aliyunsdkcore.profile import region_provider
  2. from aliyunsdkcore import client
  3. from aliyunsdkoa.request.v20170308 import CreateAdPlanRequest
  4. import json
  5. region_provider.modify_point('OA','cn-hangzhou','okapi.aliyuncs.com')
  6. clt = client.AcsClient({accessId},{accessKey},'cn-hangzhou')#添加accessIdaccessKey
  7. request = CreateAdPlanRequest.CreateAdPlanRequest()
  8. request.add_header('org-adid',{advId})#设置推广主id
  9. request.add_header('Content-Type','application/json')
  10. data= {
  11. 'ad_plan_name': 'abc'
  12. }
  13. encode_json = json.dumps(data)
  14. bits = str.encode(encode_json)
  15. request.set_content(bits)
  16. print request.get_content()
  17. request.set_accept_format('JSON')
  18. result = clt.do_action(request)
  19. print result

获取推广计划:

  1. from aliyunsdkcore import client
  2. from aliyunsdkoa.request.v20170308 import GetAdPlanListRequest
  3. from aliyunsdkcore.profile import region_provider
  4. if __name__ == '__main__':
  5. region_provider.modify_point('OA','cn-hangzhou','okapi.aliyuncs.com')
  6. clt = client.AcsClient({accessId},{accessKey},'cn-hangzhou')#添加accessIdaccessKey
  7. request=GetAdPlanListRequest.GetAdPlanListRequest()
  8. request.set_accept_format('JSON')
  9. request.add_header('org-adid',{advId})#设置推广主id
  10. #request.set_query_params({'dsp_query':'true'})#查询广告公司下所有推广计划需要打开,查询单个推广主时不需要
  11. result = clt.do_action(request)
  12. print result

获取当个计划:

  1. from aliyunsdkcore.profile import region_provider
  2. from aliyunsdkcore import client
  3. from aliyunsdkoa.request.v20170308 import GetAdPlanRequest
  4. import json
  5. region_provider.modify_point('OA','cn-hangzhou','okapi.aliyuncs.com')
  6. clt = client.AcsClient({accessId},{accessKey},'cn-hangzhou')
  7. request = GetAdPlanRequest.GetAdPlanRequest()
  8. request.set_id('250')
  9. request.add_header('org-adid',{advId}})#设置推广主id
  10. request.set_accept_format('JSON')
  11. response_json = clt.do_action(request)
  12. print response_json

创建推广组:

  1. from aliyunsdkcore.profile import region_provider
  2. from aliyunsdkcore import client
  3. from aliyunsdkoa.request.v20170308 import CreateAdGroupRequest
  4. import json
  5. region_provider.modify_point('OA','cn-hangzhou','okapi.aliyuncs.com')
  6. clt = client.AcsClient({accessId},{accessKey},'cn-hangzhou')#添加accessIdaccessKey
  7. request = CreateAdGroupRequest.CreateAdGroupRequest()
  8. request.add_header('org-adid',{advId})#设置推广主id
  9. request.add_header('Content-Type','application/json')
  10. data= { 'ad_plan_id': 1,
  11. 'ad_group_name': 'abc',
  12. 'ad_bid_type': 0,
  13. 'ad_bid_price': '12',
  14. 'ad_source_type': '1',
  15. 'view_type_ids': '101',
  16. 'group_type': 0,
  17. 'mobile_source_type': 2
  18. }
  19. encode_json = json.dumps(data)
  20. bits = str.encode(encode_json)
  21. request.set_content(bits)
  22. print request.get_content()
  23. request.set_accept_format('JSON')
  24. result = clt.do_action(request)
  25. print result

获取推广组

  1. from aliyunsdkcore.profile import region_provider
  2. from aliyunsdkcore import client
  3. from aliyunsdkoa.request.v20170308 import GetAdGroupListRequest
  4. import json
  5. region_provider.modify_point('OA','cn-hangzhou','okapi.aliyuncs.com')
  6. clt = client.AcsClient({accessId},{accessKey},'cn-hangzhou')#添加accessIdaccessKey
  7. request = GetAdGroupListRequest.GetAdGroupListRequest()
  8. request.add_header('org-adid',{advId}) #设置推广主id
  9. #request.set_query_params({'dsp_query':'true'})#查询广告公司下所有推广组需要打开,查询单个推广主时不需要
  10. request.set_query_params({'ad_plan_id':'1111111111'})
  11. request.set_accept_format('JSON')
  12. response_json = clt.do_action(request)
  13. print response_json

获取计划下所有推广组:

  1. from aliyunsdkcore.profile import region_provider
  2. from aliyunsdkcore import client
  3. from aliyunsdkoa.request.v20170308 import GetAdGroupListRequest
  4. import json
  5. region_provider.modify_point('OA','cn-hangzhou','okapi.aliyuncs.com')
  6. clt = client.AcsClient({accessId},{accessKey},'cn-hangzhou')#添加accessIdaccessKey
  7. request = GetAdGroupListRequest.GetAdGroupListRequest()
  8. request.add_header('org-adid',{advId})#设置推广主id
  9. request.set_query_params({'ad_plan_id':{adPlanId}})#url后面的查询参数
  10. request.set_accept_format('JSON')
  11. response_json = clt.do_action(request)
  12. print response_json

创建创意:

  1. from aliyunsdkcore import client
  2. from aliyunsdkoa.request.v20170308 import CreateAdCreativeRequest
  3. from aliyunsdkcore.profile import region_provider
  4. import json
  5. if __name__ == '__main__':
  6. region_provider.modify_point('OA','cn-hangzhou','okapi.aliyuncs.com')
  7. clt = client.AcsClient({accessId},{accessKey},'cn-hangzhou')#添加accessIdaccessKey
  8. request=CreateAdCreativeRequest.CreateAdCreativeRequest()
  9. request.add_header('org-adid',{advId})#设置推广主id
  10. request.add_header('Content-Type','application/json')
  11. data= {"ad_group_id" : 160,"creative_name" : "abc","img_path" : "http://200x200.jpg","dest_url" : "http://www.aliyun.com","wechat_url" : "hhh","img_size" : "200*200","category":90653}
  12. encode_json = json.dumps(data)
  13. print encode_json
  14. bits = str.encode(encode_json)
  15. request.set_content(bits)
  16. print bytes.decode(bits)
  17. request.set_accept_format('JSON')
  18. result = clt.do_action(request)
  19. print result

获取所有创意列表

  1. from aliyunsdkcore import client
  2. from aliyunsdkoa.request.v20170308 import GetAdChectCreativeListRequest
  3. from aliyunsdkcore.profile import region_provider
  4. import json
  5. if __name__ == '__main__':
  6. region_provider.modify_point('OA','cn-hangzhou','okapi.aliyuncs.com')
  7. clt = client.AcsClient(${accessId},${accessKey},'cn-hangzhou')#添加accessIdaccessKey
  8. request=GetAdChectCreativeListRequest.GetAdChectCreativeListRequest()
  9. request.set_accept_format('JSON')
  10. result = clt.do_action(request)
  11. print result