API Upload

From TwippleWiki

Jump to: navigation, search

Contents

Lang English / 日本語

Summary

This API acquires "Image data" and "Transfer of the user information reference authority by OAuth Echo" via HTTP and accepts the contribution. And, URL of the contributed image is returned with XML.

The contribution of tweet is not done in this API, and when the image page is displayed, acquired from the Twitter Co.. When this API is used, the knowledge of RESTful API and OAuth Echo is needed.

This API mounted OAuth Echo to include URL that has been signed to verify_credential in the request. This API is expediently called v1 in twipple. Another API is mounted to include the request header,and it is called v2 on this site and distinguished.

Detail

Endpoint

http://p.twipple.jp/api/upload

Parameter

Field name Corresponding values Required field Description
media binary data Yes Image data from which multipart/form-data processing is given to either of gif, jpg or png format (4MB or less)
verify_url signed url Yes URL that has signed Twitter to verify_credential. User Twitter API 1.1's URL. See https://dev.twitter.com/docs/api/1.1/get/account/verify_credentials
auth oauth No Exists for other API compatibility. Optional
username twitter screen_name No Exists for other API compatibility. Optional
upload_from - No Identifier to identify the Upload Client. Optional

Response

Success

ImageID and URL including it are returned.

 <?xml version="1.0" encoding="UTF-8"?>
 <rsp stat="ok">
     <mediaid>XYZ012</mediaid>
     <mediaurl>http://p.twipple.jp/XYZ012</mediaurl>
 </rsp>

Failure

The error code and the message are returned.

 <?xml version="1.0" encoding="UTF-8"?>
 <rsp stat="fail">
     <err code="0001" msg="error message" />
 </rsp>
Error Code
error code detail
1001 Invalid twitter username or password.
1002 Image not found.
1004 Image larger than 4MB.

Related Link

Sample Code

An actual contribution method using "upload api v1" is described.

PHP Sample

It is possible to contribute to "Twipple photo" by the following PHP codes.

<?php
//abraham OAuth
//http://github.com/abraham/twitteroauth
require_once('OAuth.php');
 
// fix your application setting
define(CONSUMER_KEY, '[YOUR CONSUMER KEY]');
define(CONSUMER_SECRET, '[YOUR CONSUMER SECRET]');
define(TOKEN_KEY, '[CLIENT TOKEN]');
define(TOKEN_SECRET, '[CLIENT SECRET]');
 
$sha1_sig = new OAuthSignatureMethod_HMAC_SHA1();
$consumer = new OAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
$token    = new OAuthConsumer(TOKEN_KEY, TOKEN_SECRET);
 
$request = OAuthRequest::from_consumer_and_token($consumer, $token, 'GET', 
	'https://api.twitter.com/1.1/account/verify_credentials.json', array());
$request->sign_request($sha1_sig, $consumer, $token);
 
$signed_url = $request->to_url();
 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://p.twipple.jp/api/upload');
 
$post = array (
    'verify_url' => $signed_url      // signed url
    'media' => '@/path/to/image.jpg' // upload media file
);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
 
$response = curl_exec($ch);
curl_close ($ch);
 
var_dump($response);

Python Sample

It is possible to contribute to "Twipple photo" by the following python codes.

import oauth2 as oauth
# simplegeo / python-oauth2
# https://github.com/simplegeo/python-oauth2
import time, pycurl
 
consumer = oauth.Consumer(key="[YOUR CONSUMER KEY]",  secret="[YOUR CONSUMER SECRET]")
token    = oauth.Token(key="[CLIENT TOKEN]",  secret="[CLIENT SECRET]")
 
url = "https://api.twitter.com/1.1/account/verify_credentials.json"
 
req = oauth.Request(method="GET",  url=url,  parameters={
        'oauth_version': "1.0",
        'oauth_nonce': oauth.generate_nonce(),
        'oauth_timestamp': int(time.time()),
        'oauth_token': token.key,
        'oauth_consumer_key': consumer.key
        })
 
signature_method = oauth.SignatureMethod_HMAC_SHA1()
req.sign_request(signature_method,  consumer,  token)
 
c = pycurl.Curl()
c.setopt(c.URL,  "http://p.twipple.jp/api/upload")
c.setopt(c.POST,  1)
c.setopt(c.VERBOSE,  1)
c.setopt(c.HTTPPOST,  [
        ('auth',  'oauth'),
        ('verify_url',  req.to_url()),
        ('media',  (c.FORM_FILE,  '/path/to/image.jpg'))
        ])
c.perform()
c.close()

curl Sample

In curl, preparing URL that has been signed becomes assumption. This is effective for Debug.

curl -v -F "media=@/path/to/image.jpg" 
-F "verify_url=https://api.twitter.com/1.1/account/verify_credentials.json?oauth_consumer_key=[CONSUMER KEY]
&oauth_nonce=[NONCE]&
oauth_signature=[SIGNATURE]&
oauth_signature_method=HMAC-SHA1&
oauth_timestamp=[timestamp]&
oauth_token=[OAUTH TOKEN]&oauth_version=1.0" 
 http://p.twipple.jp/api/upload

It is actually one line though changes line for inspection.


Lang English / 日本語
Namespaces
Variants
Views
Actions
Navigation
API