Howto use python urllib2 to create service POST with bitbucket API?

老子叫甜甜 提交于 2019-12-06 13:31:37

问题


While this code works fine to add a deployment ssh-key to my repos...

 print 'Connecting to Bitbucket...'
 bitbucket_access = base64.b64encode(userbb + ":" + passwordbb)
 bitbucket_headers = {"Content-Type":"application/json", "Authorization":"Basic " + bitbucket_access}
 bitbucket_request_url = "https://bitbucket.org/api/1.0/repositories/<username>/%s/deploy-keys" % project_name
 bitbucket_request_req = urllib2.Request(bitbucket_request_url)

 for key,value in bitbucket_headers.items():
     bitbucket_request_req.add_header(key,value)

 request_data = json.dumps({"key":public_key, "label":subproject})
 request_response = urllib2.urlopen(bitbucket_request_req, request_data)

... I cannot do the same to create a service hook.

 print 'Connecting to Bitbucket...'
 bitbucket_access = base64.b64encode(userbb + ":" + passwordbb)
 bitbucket_headers = {"Content-Type":"application/json", "Authorization":"Basic " + bitbucket_access}
 bitbucket_request_url = "https://bitbucket.org/api/1.0/repositories/<username>/%s/services" % project_name
 bitbucket_request_req = urllib2.Request(bitbucket_request_url)

 for key,value in bitbucket_headers.items():
     bitbucket_request_req.add_header(key,value)

 request_data = json.dumps({"type":"POST","URL":webhook})
 request_response = urllib2.urlopen(bitbucket_request_req, request_data)

As it gives me 404

urllib2.HTTPError: HTTP Error 404: NOT FOUND

But I can create a webhook manually using curl like this:

curl -X POST -u ${USERNAME}:${PASSWORD} https://bitbucket.org/api/1.0/repositories/<username>/<repository>/services --data 'type=POST&URL=https%3A%2F%2Fmydomain.com%2Fwebhook'

So how could I use python urllib2 to successfully add a webhook URL to my repository in Bitbucket API?

来源:https://stackoverflow.com/questions/18935149/howto-use-python-urllib2-to-create-service-post-with-bitbucket-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!