1. 程式人生 > >新浪釋出帶圖片的微博[multipart/form-data格式上傳檔案]

新浪釋出帶圖片的微博[multipart/form-data格式上傳檔案]

在使用新浪微博的API更新一條帶圖片的微博時,需要使用multipart/form-data風格的POST

需要在request頭設定Content-type

Content-type = multipart/form-data; boundary=xxxx

其中 boundary=xxxx很重要,是在request的body中構造分隔符的標識

下面這是使用httpfox擷取的request的body部分,即post的資料部分

--xxxx
Content-Disposition: form-data; name="status"

111
--xxxx
Content-Disposition: form-data; name="pic"; filename="shell.png"
Content-Type: image/png

圖片內容
--xxxx--

在xxxx前面加上兩個’-‘構成了分隔行

在xxxx前後分別加上兩個’-‘就構成了結束行

另外還要注意下,換行是'\r\n',而且正式內容和說明之間還隔了一個空行

下面是使用python上傳帶圖片的微博

def requestMultiPart(self,URI,params):
		'''
			params =[{'name':'pic','filename':'shell.png','type':'image/png','data':'xxx'},...]
			
			if 'error' in return dict,thus the request was failed!
		'''
		if self.user_token==None or self.user_secret==None:
			return False,'token or secret is None' 
		header = [
        ('oauth_consumer_key', self.APP_KEY),
        ('oauth_nonce', uuid.uuid4().hex),
        ('oauth_signature_method', 'HMAC-SHA1'),
        ('oauth_timestamp', int(time.time())),
        ('oauth_version', '1.0'),
        ('oauth_token',self.user_token)
    	]
		header2 = header[:]
		header2.sort()
		p = 'POST&%s&%s' % (quote(URI, safe=''), quote(urlencode(header2)))
		signature = hmac.new(self.APP_SECRET + '&' + self.user_secret, p, hashlib.sha1).digest().encode('base64').rstrip()
		header.append(('oauth_signature', quote(signature)))
		header = ', '.join(['%s="%s"' % (k, v) for (k, v) in header])
		header = {'Authorization': 'OAuth realm="", %s' % header}
		header['Content-type'] ='multipart/form-data; boundary=huohua'
		
		data = ''
		for param in params:
			data += '--huohua\r\n'
			data += 'Content-Disposition: form-data; name="%s"; '%param['name']
			if 'filename' in param:
				data += 'filename="%s"'%param['filename']
			if 'type' in param:
				data += '\r\nContent-Type: %s'%param['type']
			data +='\r\n\r\n'
			data += param['data']
			data +='\r\n'
		data += '--huohua--'
		print data
		try:
			request = urllib2.Request(URI,data=data, headers=header)
			result = urllib2.urlopen(request).read().decode('utf-8')
			result = json.loads(result)
		except urllib2.HTTPError,e:
			result = {'error':str(e.code)+' ' + e.msg}
		except:
			result = {'error':'get respose from sina error: request error |'+traceback.format_exc()}
		
		return result
def statuses_upload(self,status,imgpath):
		'''
		upload image and update statuses
		'''
		if not os.path.exists(imgpath):
			return {'error':'image file not exist!'}
		filename = os.path.basename(imgpath)
		type = filename.split('.')[-1]
		f = open(imgpath,'rb')
		pic = f.read()
		print len(pic)
		f.close()
		requst = self.sinaRequest.requestMultiPart('http://api.t.sina.com.cn/statuses/upload.json',\
				[{'name':'status','data':status},{'name':'pic','filename':filename,'type':'image/'+type,'data':pic}])
		
		return requst