Created
March 24, 2013 19:57
-
-
Save sherief/5233271 to your computer and use it in GitHub Desktop.
Tumblr C++ photo upload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <oauth/oauth.h> | |
#include <sys/socket.h> | |
#include <curl/curl.h> | |
#include <iostream> | |
#include <fstream> | |
#include <vector> | |
#include <cstdio> | |
using namespace std; | |
const char* const ConsumerKey = "x"; | |
const char* const ConsumerSecret = "x"; | |
std::string TokenKey = "x"; | |
std::string TokenSecret = "x"; | |
void function_pt(void *ptr, size_t size, size_t nmemb, void *stream) | |
{ | |
printf("%s", ptr); | |
} | |
std::string oauth_header(std::string In, const std::string& HTTPMethod = "GET", const std::string& PostFields = "") | |
{ | |
int argc; | |
char **argv = NULL; | |
if(HTTPMethod == "GET") | |
{ | |
argc = oauth_split_url_parameters(In.c_str(), &argv); | |
if (1) { | |
int i; | |
for (i=0;i<argc; i++) | |
{ | |
//printf("%d:%s\n", i, argv[i]); | |
} | |
} | |
} | |
if(HTTPMethod == "POST") | |
{ | |
auto Temp = (In + "?" + PostFields); | |
cout << Temp << endl; | |
argc = oauth_split_url_parameters(Temp.c_str(), &argv); | |
if (1) { | |
int i; | |
for (i=0;i<argc; i++) | |
{ | |
printf("%d:%s\n", i, argv[i]); | |
} | |
} | |
} | |
oauth_sign_array2_process(&argc, &argv, | |
NULL, //< postargs (unused) | |
OA_HMAC, | |
HTTPMethod.c_str(), //< HTTP method (defaults to "GET") | |
ConsumerKey, ConsumerSecret, TokenKey.c_str(), TokenSecret.c_str()); | |
// 'default' oauth_sign_url2 would do: | |
// req_url = oauth_serialize_url(argc, 0, argv); | |
// we split [x_]oauth_ parameters (for use in HTTP Authorization header) | |
auto req_hdr = oauth_serialize_url_sep(argc, 1, argv, ", ", 6); | |
// and other URL parameters | |
auto req_url = oauth_serialize_url_sep(argc, 0, argv, "&", 1); | |
oauth_free_array(&argc, &argv); | |
// done with OAuth stuff, now perform the HTTP request. | |
char* http_hdr = (char*)malloc(strlen(req_hdr) + 55); | |
// Note that (optional) 'realm' is not to be | |
// included in the oauth signed parameters and thus only added here. | |
// see 9.1.1 in http://oauth.net/core/1.0/#anchor14 | |
sprintf(http_hdr, "Authorization: OAuth realm=\"http://api.tumblr.com/\", %s", req_hdr); | |
//printf("request URL=%s\n", req_url); | |
//printf("request header=%s\n\n", http_hdr); | |
//auto reply = oauth_http_get2(req_url,NULL, http_hdr); | |
string Result = http_hdr; | |
if(req_url) free(req_url); | |
if(req_hdr) free(req_hdr); | |
if(http_hdr)free(http_hdr); | |
//if(reply) free(reply); | |
return Result; | |
} | |
bool do_get(CURL* m_curlHandle, std::string getUrl, const std::string& HTTPMethod = "GET", const std::string& PostFields = "") | |
{ | |
std::string dataStrDummy; | |
std::string oAuthHttpHeader; | |
struct curl_slist* pOAuthHeaderList = NULL; | |
using namespace std; | |
/* Prepare standard params */ | |
//prepareStandardParams(); | |
/* Set OAuth header */ | |
oAuthHttpHeader = oauth_header(getUrl, HTTPMethod, PostFields); | |
if( oAuthHttpHeader.length()) | |
{ | |
pOAuthHeaderList = curl_slist_append( pOAuthHeaderList, oAuthHttpHeader.c_str() ); | |
if( pOAuthHeaderList ) | |
{ | |
curl_easy_setopt( m_curlHandle, CURLOPT_HTTPHEADER, pOAuthHeaderList ); | |
} | |
} | |
//cout << oAuthHttpHeader << endl; | |
/* Set http request and url */ | |
if(HTTPMethod == "GET") | |
{ | |
curl_easy_setopt( m_curlHandle, CURLOPT_HTTPGET, 1 ); | |
} | |
if(HTTPMethod == "POST") | |
{ | |
curl_easy_setopt( m_curlHandle, CURLOPT_HTTPPOST, 1 ); | |
curl_easy_setopt(m_curlHandle, CURLOPT_POSTFIELDS, PostFields.c_str()); | |
} | |
curl_easy_setopt( m_curlHandle, CURLOPT_URL, getUrl.c_str() ); | |
curl_easy_setopt(m_curlHandle, CURLOPT_WRITEFUNCTION, function_pt); | |
curl_easy_setopt(m_curlHandle, CURLOPT_VERBOSE, 1); | |
/* Send http request */ | |
if( CURLE_OK == curl_easy_perform( m_curlHandle ) ) | |
{ | |
if( pOAuthHeaderList ) | |
{ | |
curl_slist_free_all( pOAuthHeaderList ); | |
} | |
return true; | |
} | |
if( pOAuthHeaderList ) | |
{ | |
curl_slist_free_all( pOAuthHeaderList ); | |
} | |
return false; | |
} | |
std::string to_hex(int x) | |
{ | |
char res[3] = {}; /* two bytes of hex = 4 characters, plus NULL terminator */ | |
if (x <= 0xFF) | |
{ | |
sprintf(&res[0], "%02x", x); | |
} | |
return res; | |
} | |
int main(int argc, const char * argv[]) | |
{ | |
CURL *curl; | |
CURLcode res; | |
curl = curl_easy_init(); | |
// | |
ifstream Image("image.jpg", ios::binary); | |
vector<unsigned char> data; | |
char T; | |
while(Image.get(T)) | |
{ | |
data.push_back(T); | |
} | |
Image.close(); | |
unsigned char* DataPtr = &data[0]; | |
auto Size = data.size(); | |
string DataString = ""; | |
for(auto C : data) | |
{ | |
if(C == ' ') | |
{ | |
DataString += '+'; | |
} | |
else | |
{ | |
DataString += '%' + to_hex(C); | |
} | |
} | |
DataString = oauth_url_escape(DataString.c_str()); | |
do_get(curl, "http://api.tumblr.com/v2/blog/sherieffarouk.tumblr.com/post", "POST", "type=photo&data=" + DataString); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment