5

我在 python 中有一个使用 2-legged oauth2 的工作 GET。这是工作获取代码:

进口:

import oauth2 
import urllib #for url-encode
import urllib2 #for getting and receiving data from server
import time #Unix timestamp import oauth2

电话:

resourceUrl = "https://test.mysite:8443/ess/scheduleapi/v1/people"
request = build_request(resourceUrl,'GET')
u = urllib2.urlopen(request.to_url())
people_data = u.read()

构建请求的函数:

def build_request(url, method):
    params = {                                            
        'oauth_version': "1.0",
        'oauth_nonce': oauth2.generate_nonce(),
        'oauth_timestamp': int(time.time())
    }
    consumer = oauth2.Consumer(key='mykey',secret='mysecret')
    params['oauth_consumer_key'] = consumer.key
    req = oauth2.Request(method=method, url=url, parameters=params)
    signature_method = oauth2.SignatureMethod_HMAC_SHA1()
    req.sign_request(signature_method, consumer, None)
    return req
#end build_request

所以,我想我可以复制我认为需要的 GET 部分,并将它与我从一些 urllib2 文档中得到的语法结合起来,然后制作一个有效的 POST。不是这样。请记住,我有相同的导入和相同的 build_request 函数。这是BROKEN POST 代码。请指教!

电话:

myurl =  "https://test.mysite:8443/ess/scheduleapi/v1/people" 
myaction = 'POST'
myxml = somexmlIalreadygenerated
person_added, err = post_or_put_me(myaction,myxml,myurl)

POST的功能:

def post_or_put_me(action,xml,url)
    request = build_request(url,action) # use same header-generating code as GET did?
    post_data = urllib.urlencode(xml)
    req = urllib2.Request(request,post_data)
    try:
        u = urllib2.urlopen(req)
        post_or_put_returned_data = u.read()
        print 'LENGTH  :', len(post_or_put_returned_data)
        print 'DATA    :', post_or_put_returned_data
    except urllib2.HTTPError, e:
        server_error = 'HTTPError = ' + str(e.code)
    except urllib2.URLError, e:
        server_error = 'URLError = ' + str(e.reason)
    except httplib.HTTPException, e:
        server_error = 'HTTPException'
    except Exception:
        import traceback
        server_error = 'generic exception: ' + traceback.format_exc()
    #endtry

    if server_error:
        err_msg = server_error
    else:   
        succ_msg = 'you had a successful post or put'
    #endif

    return succ_msg, err_msg
#end post_or_put_me

这是我的第二次尝试:

def post_or_put_me(action,xml,url):
    myrequest = build_request(url,'POST')

    CONSUMER_KEY = 'admin_access'
    CONSUMER_SECRET = 'xxxxxxxxxx' 
    consumer = oauth2.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
    token = oauth2.Token(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
    client = oauth2.Client(consumer, token)
    resp, content = client.request(
        url,
        method=action,
        body=urllib.urlencode(str(xml)),
        headers= myrequest.headers,
        force_auth_header=True,
    )
    print 'resp, content are', resp, content
4

2 回答 2

6

这是我如何让我的 POST 或 PUT 工作的实际工作代码,由 Echo360 Lecture Capture 的 Wes Barnes 提供。我不希望其他人做 2-legged oauth POST/PUT 不得不重新发明轮子。

import oauth2 as oauth
import time
import urllib2 as urllib

echo_base_url = 'http://pilot.echo360.com/ess/scheduleapi/v1'

consumer = oauth.Consumer(key ='xxxxx', secret='xxxx')
client = oauth.Client(consumer)
params = "<person><first-name>Jon</first-name><last-name>Doe</last-name><title>Super  Hero</title><email-address>jdoe17@echo360.com</email-address><block-alerts>false</block-alerts><time-zone>US/Eastern</time-zone><locale>en_US</locale><credentials><user-name>jdoe17@echo360.com</user-name><password>password</password></credentials><organization-roles><organization-role><organization-id>b1973c39-dc76-4cab-a4aa-3f9efd628df2</organization-id><role>role-name-admin</role></organization-role></organization-roles></person>"

resp, content = client.request(
                echo_base_url + "/people/",
                method = "PUT",
                body=params,
                headers={'Content-type': 'application/xml'}
                #force_auth_header=True
                )
print resp, content
于 2012-10-31T20:46:22.297 回答
2

这是我一直在使用 oauth2 向 Twitter 发出 POST 请求的代码。希望它可以帮助您弄清楚语法。

import oauth2 as oauth, urllib

def oauth_req(url, key, secret, http_method="POST", post_body=None, http_headers=None):
    CONSUMER_KEY = YOUR_KEY
    CONSUMER_SECRET = YOUR_SECRET
    consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
    token = oauth.Token(key=key, secret=secret)
    client = oauth.Client(consumer, token)
    resp, content = client.request(
        url,
        method=http_method,
        body=urllib.urlencode({'status': post_body}),
        headers=http_headers,
        force_auth_header=True,
    )
    return content

oauth_req('http://api.twitter.com/1/statuses/update.json', KEY, SECRET, post_body=MESSAGE)
于 2012-10-19T18:44:54.083 回答