2 * @brief experimental code to sign data uploads
3 * @file oauthimageupload.c
4 * @author Robin Gareus <robin@gareus.org>
6 * Copyright 2008, 2012 Robin Gareus <robin@gareus.org>
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
37 * example oauth body signature and HTTP-POST.
38 * WARNING: <b> This is request type is not part of the
41 * This is an experimental extension.
43 int oauth_image_post(char *filename, char *url) {
44 const char *c_key = "key"; //< consumer key
45 const char *c_secret = "secret"; //< consumer secret
46 char *t_key = NULL; //< access token key
47 char *t_secret = NULL; //< access token secret
53 char *filedata = NULL;
61 // get acces token - see oautexample.c
62 t_key = strdup("key"); //< access token key
63 t_secret = strdup("secret"); //< access token secret
65 // read raw data to sign and send from file.
66 F= fopen(filename, "r");
68 fseek(F, 0L, SEEK_END);
73 filedata=mmap(NULL,filelen,PROT_READ,MAP_SHARED,fileno(F),0L);
75 filedata=malloc(filelen*sizeof(char));
76 if (filelen != fread(filedata,sizeof(char), filelen, F)) {
84 okey = oauth_catenc(2, c_secret, t_secret);
85 sign = oauth_sign_hmac_sha1_raw(filedata,filelen,okey,strlen(okey));
87 sig_url = malloc(63+strlen(url)+strlen(sign));
88 sprintf(sig_url,"%s&xoauth_body_signature=%s&xoauth_body_signature_method=HMAC_SHA1",url, sign);
90 // sign a POST request
91 req_url = oauth_sign_url2(sig_url, &postarg, OA_HMAC, NULL, c_key, c_secret, t_key, t_secret);
94 // append the oauth [post] parameters to the request-URL!!
95 sig_url = malloc(2+strlen(req_url)+strlen(postarg));
96 sprintf(sig_url,"%s?%s",req_url, postarg);
98 printf("POST:'%s'\n",sig_url);
99 //reply = oauth_post_file(sig_url,filename,filelen,"Content-Type: image/jpeg;");
100 printf("reply:'%s'\n",reply);
102 if(req_url) free(req_url);
103 if(postarg) free(postarg);
104 if(reply) free(reply);
105 if(t_key) free(t_key);
106 if(t_secret) free(t_secret);
109 munmap(filedata,filelen);
112 if(filedata) free(filedata);
119 * Main Test and Example Code.
122 * gcc -lssl -loauth -o oauthdatapost oauthdatapost.c
125 int main (int argc, char **argv) {
126 char *base_url = "http://mms06.test.mediamatic.nl";
127 char *filename = "/tmp/test.jpg";
129 char *title = "test";
132 if (argc>4) base_url = argv[4];
133 if (argc>3) title = argv[3];
134 if (argc>2) anyid = atoi(argv[2]);
135 if (argc>1) filename = argv[1];
137 // TODO check if file exists; also read oauth-params from args or file
139 // anyMeta.nl image-post module URL
140 url = malloc(1024*sizeof(char));
141 if (anyid<1 && !title)
142 sprintf(url,"%s/module/ImagePost/",base_url);
143 else if (anyid>0 && !title)
144 sprintf(url,"%s/module/ImagePost/%i?echoid=1",base_url,anyid);
145 else if (anyid<1 && title) {
146 char *tp = oauth_url_escape(title);
147 sprintf(url,"%s/module/ImagePost/?title=%s",base_url,tp);
150 else if (anyid>0 && title) {
151 char *tp = oauth_url_escape(title);
152 sprintf(url,"%s/module/ImagePost/%i?echoid=1&title=%s",base_url,anyid,tp);
157 switch(oauth_image_post(filename, url)) {
159 printf("request ok.\n");
162 printf("upload failed.\n");