API Upload2

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 header. This API is expediently called v2 in twipple. Another API is mounted to include the request, and it is called v1 on this site and distinguished.

Detail

Endpoint

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

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)
upload_from - No Identifier to identify the Upload Client. Optional

Request header(in case of using Twitter API v1)

Twitter API v1 will be expired soon. Please use Twitter API v1.1.

Request header(in case of using Twitter API v1.1)

Header Name Corresponding values Required field Description
X-AUTH-SERVICE-PROVIDER URL to AUTH PROVIDER Yes https://api.twitter.com/1.1/account/verify_credentials.json Fixation
X-VERIFY-CREDENTIALS-AUTHORIZATION Signature information to AUTH PROVIDER Yes It describes it later

X-VERIFY-CREDENTIALS-AUTHORIZATION Details

Signature information is as follows.

OAuth realm="http://api.twitter.com/",
oauth_consumer_key="[YOUR CONSUMER KEY]",
oauth_signature_method="HMAC-SHA1",
oauth_token="[OAUTH TOKEN]",
oauth_timestamp="[TIMESTAMP]",
oauth_nonce="[NONCE]",
oauth_version="1.0",
oauth_signature="[SIGNATURE]"

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 v2" 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();
parse_str(parse_url($signed_url, PHP_URL_QUERY), $signed_query);
 
$header = array(
    'X-Auth-Service-Provider: https://api.twitter.com/1.1/account/verify_credentials.json',
    'X-Verify-Credentials-Authorization: OAuth realm="http://api.twitter.com/",' .
    sprintf('oauth_consumer_key="%s",', urlencode($signed_query['oauth_consumer_key'])) .
    sprintf('oauth_nonce="%s",', urlencode($signed_query['oauth_nonce'])) .
    sprintf('oauth_signature="%s",', urlencode($signed_query['oauth_signature'])) .
    'oauth_signature_method="HMAC-SHA1",' .
    sprintf('oauth_timestamp="%s",', urlencode($signed_query['oauth_timestamp'])) .
    sprintf('oauth_token="%s",', urlencode($signed_query['oauth_token'])) .
    'oauth_version="1.0"'
);
 
 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://p.twipple.jp/api/upload2');
 
$post = array (
    'media' => '@/path/to/image.jpg' // upload media file
);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
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)
 
header = [
    'X-Auth-Service-Provider: ' + url,
    'X-Verify-Credentials-Authorization: ' + req.to_header('http://api.twitter.com/')['Authorization']
]
 
c = pycurl.Curl()
c.setopt(c.URL,  "http://p.twipple.jp/api/upload2")
c.setopt(c.POST,  1)
c.setopt(c.HTTPHEADER, header)
c.setopt(c.VERBOSE,  1)
c.setopt(c.HTTPPOST,  [
        ('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 
-H 'X-Auth-Service-Provider: https://api.twitter.com/1.1/account/verify_credentials.json' 
-H 'X-Verify-Credentials-Authorization: OAuth realm="http://api.twitter.com/", oauth_consumer_key="[CONSUMER KEY]", 
oauth_signature_method="HMAC-SHA1", oauth_token="[OAUTH TOKEN]", oauth_timestamp="[TIMESTAMP]", oauth_nonce="[NONCE]", 
oauth_version="1.0", oauth_signature="[SIGNATURE]"' 
-F "media=@/path/to/image.jpg" http://p.twipple.jp/api/upload2

It is actually one line though changes line for inspection.

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