CloudSight上的OAuth1(python)

时间:2015-08-25 13:48:49

标签: python oauth cloudsight

根据official documentation,我必须使用OAuth1进行身份验证才能使用其API。我似乎无法获得认证的所有必要部分。到目前为止,这是我的代码:

#!usr/bin/env python
#encoding=utf-8

import requests
import sys, getopt
import urllib2

LOCALE = 'zh-CN'
LANGUAGE = 'zh-CN'

def doRequest(imageUrl):
    reqUrlA = 'https://api.cloudsightapi.com/image_requests/' # get token
    reqUrlB = 'https://api.cloudsightapi.com/image_responses/' # get the final recognition result with token

    headers = { 
    'Authorization' : 'CloudSight INSERT API KEY',
    'Host' : 'api.cloudsightapi.com',
    'Origin:' : 'https://cloudsightapi.com'
    }

    postData = {
    'image_request[remote_image_url]' : imageUrl,
    'image_request[locale]': LOCALE,
    'image_request[language]': LANGUAGE
    }

    try:
        response = requests.post(reqUrlA, headers=headers, data=postData)

    except Exception, e:
        print 'Error: connection error, please check your Internet and confirm the image url'
        sys.exit()

    if "error" in response.json():
        # print "Error: %s" % response.json()["error"]
        print "无法识别图片:请检查图片的连接是否合法"
        print
        sys.exit()
    else:
        token = response.json()['token']

        # you may get some response with status 'not completed' for about some times before getting the final result
        reqTimes = 20
        isNotified = True
        while reqTimes > 0:
            try:
                response = requests.get(reqUrlB + token, headers=headers)
            except Exception, e:
                print 'Error: connection error, please check your Internet and confirm the image url'
                sys.exit()
                status = response.json()['status']
                if status == 'completed':
                    print 'RESULT: '
                    print '\timage url:', imageUrl
                    print '\timage name:', response.json()['name']
                    print
                    # return response.json()['name']
                    break
                elif status == 'not completed':
                    if isNotified == True:
                        print 'recognition in progress'
                        isNotified = False
                    reqTimes -= 1



def usage():
    print '''
    usage: 
    cloudSightAPI ImageURL fdgdfgrtrgd
    type `cloudSightAPI -h` to get help
    '''

def main():
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'h')
        for op, value in opts:
            if op == '-h':
                usage()
                sys.exit()
        if len(args) == 0:
            usage()
            sys.exit()
    except getopt.GetoptError as e:
        print 'Error: using invalid parameter -%s' % e.opt
        usage()
        sys.exit()

    imageUrl = sys.argv[1]
    doRequest(imageUrl)

if __name__ == '__main__':
    main()

doRequest ("INSERT IMAGE URL")

根据official documentation,我需要获取oauth_nonce和oauth_token以创建允许我使用api的签名。我如何获得这些细节?有没有oauth创建者?

2 个答案:

答案 0 :(得分:0)

在Ruby中完成它。这简单多了!

https://github.com/cloudsight/cloudsight-ruby

答案 1 :(得分:0)

希望它能为缩进而感到遗憾,不要忘记导入请求

def quest(imageUrl):
fa=''
LOCALE = 'en-US'
LANGUAGE = 'en-US'
reqUrlB = 'https://api.cloudsightapi.com/image_responses/'
header = { 
    'Authorization' : 'CloudSight API_KEY_HERE',
    'Host' : 'api.cloudsightapi.com',
    'Origin:' : 'https://cloudsightapi.com'
    }
footer = postData = {
    'image_request[remote_image_url]' : imageUrl,
    'image_request[locale]': LOCALE,
    'image_request[language]': LANGUAGE
    }

r = requests.post("https://api.cloudsightapi.com/image_requests",headers=header,data=footer)
print r
token = r.json()['token']
status='lol'
while True:
    response = requests.get(reqUrlB + token, headers=header)
    status = response.json()['status']
    if status=='completed':
        name=response.json()['name']
        fa=name
        break
return fa