| 1 | #define _CRT_SECURE_NO_DEPRECATE
|
|---|
| 2 | #ifdef _MSC_VER1
|
|---|
| 3 | #include <windows.h>
|
|---|
| 4 | #include <wininet.h>
|
|---|
| 5 | #else
|
|---|
| 6 | #include <curl/curl.h>
|
|---|
| 7 | #include <errno.h>
|
|---|
| 8 | #endif
|
|---|
| 9 | #include <string.h>
|
|---|
| 10 | #include <sys/stat.h>
|
|---|
| 11 | #include <time.h>
|
|---|
| 12 |
|
|---|
| 13 | #include "Flickr.h"
|
|---|
| 14 | #include "XmlRpcUtils.h"
|
|---|
| 15 | #include "AtomPP.h"
|
|---|
| 16 | #include <sstream>
|
|---|
| 17 | #include <fstream>
|
|---|
| 18 |
|
|---|
| 19 | namespace Flickr {
|
|---|
| 20 |
|
|---|
| 21 | static std::string responseBuf;
|
|---|
| 22 | static int handle_returned_data(char* ptr, size_t size, size_t nmemb, void* stream)
|
|---|
| 23 | {
|
|---|
| 24 | char* pbuf = new char[size*nmemb+1];
|
|---|
| 25 | memcpy(pbuf, ptr, size*nmemb);
|
|---|
| 26 | pbuf[size*nmemb] = 0;
|
|---|
| 27 | responseBuf += pbuf;
|
|---|
| 28 | delete[] pbuf;
|
|---|
| 29 | return size*nmemb;
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | std::string getFlickrFrob(std::string appkey, std::string secret)
|
|---|
| 33 | {
|
|---|
| 34 | std::string sig;
|
|---|
| 35 | std::vector<XmlRpc::XmlRpcValue> requests;
|
|---|
| 36 | XmlRpc::XmlRpcValue::ValueStruct valuestruct;
|
|---|
| 37 | XmlRpc::XmlRpcValue response;
|
|---|
| 38 | requests.clear();
|
|---|
| 39 | valuestruct["api_key"] = appkey;
|
|---|
| 40 | sig = "";
|
|---|
| 41 | sig += "api_key";
|
|---|
| 42 | sig += appkey;
|
|---|
| 43 | sig = AtomPP::StringToHex(AtomPP::md5(secret + sig));
|
|---|
| 44 | valuestruct["api_sig"] = sig;
|
|---|
| 45 |
|
|---|
| 46 | requests.push_back(valuestruct);
|
|---|
| 47 | if (XmlRpc::execXmlRpc("http://api.flickr.com/services/xmlrpc/", "flickr.auth.getFrob", requests, response) != 0) {
|
|---|
| 48 | return "";
|
|---|
| 49 | }
|
|---|
| 50 | std::string frob = response.valueString();
|
|---|
| 51 | frob = XmlRpc::queryXml(frob, "/frob");
|
|---|
| 52 | return frob;
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | std::string getFlickrAuthUrl(std::string appkey, std::string secret, std::string frob)
|
|---|
| 56 | {
|
|---|
| 57 | std::string ret = "http://flickr.com/services/auth/";
|
|---|
| 58 | std::string data;
|
|---|
| 59 |
|
|---|
| 60 | ret += "?api_key=";
|
|---|
| 61 | data += "api_key";
|
|---|
| 62 | ret += appkey;
|
|---|
| 63 | data += appkey;
|
|---|
| 64 | ret += "&frob=";
|
|---|
| 65 | data += "frob";
|
|---|
| 66 | ret += frob;
|
|---|
| 67 | data += frob;
|
|---|
| 68 | ret += "&perms=";
|
|---|
| 69 | data += "perms";
|
|---|
| 70 | ret += "write";
|
|---|
| 71 | data += "write";
|
|---|
| 72 |
|
|---|
| 73 | std::string sig = secret;
|
|---|
| 74 | sig = AtomPP::StringToHex(AtomPP::md5(sig + data));
|
|---|
| 75 |
|
|---|
| 76 | ret += "&api_sig=";
|
|---|
| 77 | ret += sig;
|
|---|
| 78 | return ret;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | std::string getFlickrToken(std::string appkey, std::string secret, std::string frob)
|
|---|
| 82 | {
|
|---|
| 83 | std::string sig;
|
|---|
| 84 | std::vector<XmlRpc::XmlRpcValue> requests;
|
|---|
| 85 | XmlRpc::XmlRpcValue::ValueStruct valuestruct;
|
|---|
| 86 | XmlRpc::XmlRpcValue response;
|
|---|
| 87 | requests.clear();
|
|---|
| 88 | valuestruct["api_key"] = appkey;
|
|---|
| 89 | valuestruct["frob"] = frob;
|
|---|
| 90 | sig = "";
|
|---|
| 91 | sig += "api_key";
|
|---|
| 92 | sig += appkey;
|
|---|
| 93 | sig += "frob";
|
|---|
| 94 | sig += frob;
|
|---|
| 95 | sig = AtomPP::StringToHex(AtomPP::md5(secret + sig));
|
|---|
| 96 | valuestruct["api_sig"] = sig;
|
|---|
| 97 |
|
|---|
| 98 | requests.push_back(valuestruct);
|
|---|
| 99 | if (XmlRpc::execXmlRpc("http://api.flickr.com/services/xmlrpc/", "flickr.auth.getToken", requests, response) != 0) {
|
|---|
| 100 | return "";
|
|---|
| 101 | }
|
|---|
| 102 | std::string token = response.valueString();
|
|---|
| 103 | token = XmlRpc::queryXml(token, "/auth/token");
|
|---|
| 104 | return token;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | int registerFlickrToken(std::string url)
|
|---|
| 108 | {
|
|---|
| 109 | CURL* curl;
|
|---|
| 110 | CURLcode res;
|
|---|
| 111 | struct curl_slist *headerlist = NULL;
|
|---|
| 112 |
|
|---|
| 113 | curl = curl_easy_init();
|
|---|
| 114 | if (!curl) {
|
|---|
| 115 | return -2;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | std::string sig;
|
|---|
| 119 |
|
|---|
| 120 | struct curl_httppost *formpost = NULL;
|
|---|
| 121 | struct curl_httppost *lastptr = NULL;
|
|---|
| 122 | std::map<std::string, std::string> formdata;
|
|---|
| 123 |
|
|---|
| 124 | curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
|---|
| 125 | curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
|
|---|
| 126 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, handle_returned_data);
|
|---|
| 127 |
|
|---|
| 128 | responseBuf = "";
|
|---|
| 129 | res = curl_easy_perform(curl);
|
|---|
| 130 | #ifdef _DEBUG
|
|---|
| 131 | {
|
|---|
| 132 | FILE *fp = fopen("res.xml", "wb");
|
|---|
| 133 | fprintf(fp, "%s", responseBuf.c_str());
|
|---|
| 134 | fclose(fp);
|
|---|
| 135 | }
|
|---|
| 136 | #endif
|
|---|
| 137 | curl_easy_cleanup(curl);
|
|---|
| 138 |
|
|---|
| 139 | if(res != CURLE_OK) {
|
|---|
| 140 | responseBuf = "";
|
|---|
| 141 | return -3;
|
|---|
| 142 | }
|
|---|
| 143 | responseBuf = "";
|
|---|
| 144 | return 0;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | int FlickrUpload(std::string appkey, std::string secret, std::string token, std::string filename)
|
|---|
| 148 | {
|
|---|
| 149 | CURL* curl;
|
|---|
| 150 | CURLcode res;
|
|---|
| 151 | struct curl_slist *headerlist = NULL;
|
|---|
| 152 |
|
|---|
| 153 | curl = curl_easy_init();
|
|---|
| 154 | if (!curl) {
|
|---|
| 155 | return -2;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | std::string sig;
|
|---|
| 159 |
|
|---|
| 160 | struct curl_httppost *formpost = NULL;
|
|---|
| 161 | struct curl_httppost *lastptr = NULL;
|
|---|
| 162 | std::map<std::string, std::string> formdata;
|
|---|
| 163 |
|
|---|
| 164 | curl_easy_setopt(curl, CURLOPT_URL, "http://api.flickr.com/services/upload/");
|
|---|
| 165 |
|
|---|
| 166 | curl_formadd(&formpost, &lastptr,
|
|---|
| 167 | CURLFORM_COPYNAME, "api_key",
|
|---|
| 168 | CURLFORM_COPYCONTENTS, appkey.c_str(),
|
|---|
| 169 | CURLFORM_END);
|
|---|
| 170 | curl_formadd(&formpost, &lastptr,
|
|---|
| 171 | CURLFORM_COPYNAME, "auth_token",
|
|---|
| 172 | CURLFORM_COPYCONTENTS, token.c_str(),
|
|---|
| 173 | CURLFORM_END);
|
|---|
| 174 | sig = "";
|
|---|
| 175 | sig += "api_key";
|
|---|
| 176 | sig += appkey;
|
|---|
| 177 | sig += "auth_token";
|
|---|
| 178 | sig += token;
|
|---|
| 179 | sig = AtomPP::StringToHex(AtomPP::md5(secret + sig));
|
|---|
| 180 | curl_formadd(&formpost, &lastptr,
|
|---|
| 181 | CURLFORM_COPYNAME, "api_sig",
|
|---|
| 182 | CURLFORM_COPYCONTENTS, sig.c_str(),
|
|---|
| 183 | CURLFORM_END);
|
|---|
| 184 | curl_formadd(&formpost, &lastptr,
|
|---|
| 185 | CURLFORM_COPYNAME, "photo",
|
|---|
| 186 | CURLFORM_FILE, filename.c_str(),
|
|---|
| 187 | CURLFORM_CONTENTTYPE, "image/jpeg",
|
|---|
| 188 | CURLFORM_END);
|
|---|
| 189 | curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
|
|---|
| 190 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, handle_returned_data);
|
|---|
| 191 |
|
|---|
| 192 | responseBuf = "";
|
|---|
| 193 | res = curl_easy_perform(curl);
|
|---|
| 194 | curl_easy_cleanup(curl);
|
|---|
| 195 |
|
|---|
| 196 | if(res != CURLE_OK) {
|
|---|
| 197 | responseBuf = "";
|
|---|
| 198 | return -3;
|
|---|
| 199 | }
|
|---|
| 200 | std::string photoid = XmlRpc::queryXml(responseBuf, "/rsp/photoid");
|
|---|
| 201 | return 0;
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | }
|
|---|