Monday, November 29, 2010

Sharetronix oAuth REST API Integration

1. Log in to create micro-blog application read and write, get oauth_consumer_key and oauth_consumer_secret.

2. Send oauth_consumer_key and oauth_consumer_secret be oauth_token_secret and oauth_token.

$ Request_url = " http://localhost/sharetronix/oauth/request_token ";

$ Oauth_consumer_key = "dT0xJmFwcF9pZD02NTYyMzc3JmFjY2Vzcz1ydw ==";
$ Oauth_consumer_secret = "9c91d365e0ae9e8bec01bfcb6ebd97cd";
$ Oauth_timestamp = time ();
$ Nonce = md5 (rand (). Time (). Rand ());

echo $ oauth_timestamp. "
";

echo $ nonce. "
";

$ Parameters = "oauth_consumer_key =". Urlencode (utf8_encode ($ oauth_consumer_key));
$ Parameters .= "& oauth_nonce =". Urlencode (utf8_encode ($ nonce));
$ Parameters .= "& oauth_signature_method =". Urlencode (utf8_encode ("HMAC-SHA1"));
$ Parameters .= "& oauth_timestamp =". Urlencode (utf8_encode ($ oauth_timestamp));
$ Parameters .= "& oauth_version =". Urlencode (utf8_encode ("1.0"));

$ Resource_string = "GET &". Urlencode (utf8_encode ($ request_url ))."&". urlencode (utf8_encode ($ parameters));

$ Oauth_signature = base64_encode (hash_hmac ("sha1", $ resource_string, $ oauth_consumer_secret ."&", true));

$ Request_body = $ request_url. "? Oauth_nonce =". $ Nonce. "& Oauth_timestamp =". $ Oauth_timestamp;
$ Request_body .= "& oauth_consumer_key =". $ Oauth_consumer_key;
$ Request_body .= "& oauth_signature_method =". Urlencode (utf8_encode ("HMAC-SHA1"));
$ Request_body .= "& oauth_signature =". $ Oauth_signature. "& Oauth_version = 1.0";


$ My_request = curl_init ();
curl_setopt ($ my_request, CURLOPT_URL, $ request_body);
curl_setopt ($ my_request, CURLOPT_RETURNTRANSFER, TRUE);
$ Request_result = curl_exec ($ my_request);
curl_close ($ my_request);
echo $ request_result;
?>

3. By oauth_token for oauth_verifier.

http://localhost/sharetronix/oauth/authorize?oauth_token=e3c062a

4. By oauth_verifier for access_token and access_secret.

$ Request_url = " http://localhost/sharetronix/oauth/access_token ";

$ Oauth_consumer_key = "dT0xJmFwcF9pZD02NTYyMzc3JmFjY2Vzcz1y";
$ Oauth_consumer_secret = "9c91d365e0ae9e8bec01bfcb6ebd97cd";

$ Oauth_timestamp = time ();
$ Nonce = md5 (rand (). Time (). Rand ());
$ T_secret = "b6e40393eebff0202804446742fa3956"; / / TOKEN_SECRET_RECEIVED_FROM_oauth / request_token;
$ Oauth_verifier = "13e454";
$ Oauth_token = "e3c062a";
$ Oauth_timestamp = "1285752992"; / / and send the same request_token
$ Nonce = "cd15f0147d4ebecac493fa8c2735cd89"; / / and send the same request_token

$ Parameters = "oauth_consumer_key =". Urlencode (utf8_encode ($ oauth_consumer_key));
$ Parameters .= "& oauth_nonce =". Urlencode (utf8_encode ($ nonce));
$ Parameters .= "& oauth_signature_method =". Urlencode (utf8_encode ("HMAC-SHA1"));
$ Parameters .= "& oauth_timestamp =". Urlencode (utf8_encode ($ oauth_timestamp));
$ Parameters .= "& oauth_token =". Urlencode (utf8_encode ($ oauth_token));
$ Parameters .= "& oauth_verifier =". Urlencode (utf8_encode ($ oauth_verifier));
$ Parameters .= "& oauth_version =". Urlencode (utf8_encode ("1.0"));

$ Base = "POST &". Urlencode (utf8_encode ($ request_url ))."&". urlencode (utf8_encode ($ parameters));

$ Oauth_signature = base64_encode (hash_hmac ("sha1", $ base, urlencode ($ oauth_consumer_secret )."&". urlencode ($ t_secret), true));

$ Request_body = "oauth_nonce =". $ Nonce. "& Oauth_timestamp =". $ Oauth_timestamp;
$ Request_body .= "& oauth_consumer_key =". $ Oauth_consumer_key;
$ Request_body .= "& oauth_signature_method =". Urlencode (utf8_encode ("HMAC-SHA1"));
$ Request_body .= "& oauth_signature =". $ Oauth_signature. "& Oauth_version = 1.0";
$ Request_body .= "& oauth_verifier =". Urlencode (utf8_encode ($ oauth_verifier));
$ Request_body .= "& oauth_token =". Urlencode (utf8_encode ($ oauth_token));

/ / $ _GET ["Oauth_verifier"] = oauth_verifier_received_from_oauth / authenticate

$ My_request = curl_init ();
curl_setopt ($ my_request, CURLOPT_URL, $ request_url);
curl_setopt ($ my_request, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt ($ my_request, CURLOPT_POST, 1);
curl_setopt ($ my_request, CURLOPT_POSTFIELDS, $ request_body);
$ Access_request_result = curl_exec ($ my_request);
curl_close ($ my_request);

echo "access". $ access_request_result;
?> ?>

5. Operation user data.

/ / $ Request_url = " http://www.Sharetronix.com ";
$ Request_url = " http://localhost/sharetronix/1/statuses/update.json ";
$ Request_url2 = " http://localhost/sharetronix/1/statuses ";
$ Oauth_consumer_key = "dT0xJmFwcF9pZD02NTYyMzc3JmFjY2Vzcz1ydw ==";
$ Oauth_consumer_secret = "9c91d365e0ae9e8bec01bfcb6ebd97cd";
/ / For base string use the method name {/ action}. {Data_format}:
/ / Http://example.com/1/groups {/ action}. {Data_format}
/ / Http://example.com/1/saved_searches {/ action}. {Data_format}
/ / Http://example.com/1/favorites {/ action}. {Data_format}
/ / Http://example.com/1/users {/ action}. {Data_format}
/ / Etc.

$ Access_token = "7e1ffb185a75c5584f478624f149286e";
$ Access_secret = "db74a3b50a432b4d889b26b915701e04";

$ Oauth_timestamp = time ();
$ Nonce = md5 (rand (). Time (). Rand ());

/ / $ Oauth_timestamp = "1285752722";
/ / $ Nonce = "7e336846bfe439eb4cddca18cc104e7e";


$ Parameters = "oauth_consumer_key =". Urlencode (utf8_encode ($ oauth_consumer_key));
$ Parameters .= "& oauth_nonce =". Urlencode (utf8_encode ($ nonce));
$ Parameters .= "& oauth_signature_method =". Urlencode (utf8_encode ("HMAC-SHA1"));
$ Parameters .= "& oauth_timestamp =". Urlencode (utf8_encode ($ oauth_timestamp));
$ Parameters .= "& oauth_token =". Urlencode (utf8_encode ($ access_token));
$ Parameters .= "& oauth_version =". Urlencode (utf8_encode ("1.0"));

$ Resource_string = "POST &". Urlencode (utf8_encode ($ request_url2 ))."&". urlencode (utf8_encode ($ parameters));

$ Oauth_signature = $ oauth_consumer_secret;
$ Sig = base64_encode (hash_hmac ("sha1", $ resource_string,
urlencode ($ oauth_signature )."&". urlencode ($ access_secret), true));

$ Headers = array (
"Authorization: OAuth realm = \" api.example.com \ ", oauth_consumer_key = \" ". Urlencode ($ oauth_consumer_key)." \ ", Oauth_nonce = \" ". Urlencode ($ nonce)." \ ", Oauth_signature_method = \ "". urlencode ("HMAC-SHA1"). "\", oauth_timestamp = \ "". urlencode ($ oauth_timestamp). "\", oauth_token = \ "". urlencode (utf8_encode ($ access_token)). "\ ", oauth_version = \" ". urlencode (" 1.0 ")." \ ", oauth_signature = \" ". urlencode (utf8_encode ($ sig))." \ ""
);

$ My_request = curl_init ();
curl_setopt ($ my_request, CURLOPT_URL, $ request_url);
curl_setopt ($ my_request, CURLOPT_HTTPHEADER, $ headers);
curl_setopt ($ my_request, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt ($ my_request, CURLOPT_POST, 1);
curl_setopt ($ my_request, CURLOPT_POSTFIELDS, "suppress_response_codes = 1 & status = speech test 2!");
$ Contacts_request_result = curl_exec ($ my_request);
curl_close ($ my_request);

echo $ contacts_request_result;
?>

Original Post: http://hi.baidu.com/cr0_3/blog/item/7492a11ca3873304314e1563.html

1 comment: