Howto use python urllib2 to create service POST wi

2019-04-15 16:59发布

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?

0条回答
登录 后发表回答