2 * @brief experimental code to sign data uploads
3 * @file oauthbodysign.c
4 * @author Robin Gareus <robin@gareus.org>
6 * Copyright 2009, 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
32 int my_data_post(char *url, char *data) {
33 const char *c_key = "key"; //< consumer key
34 const char *c_secret = "secret"; //< consumer secret
35 char *t_key = "tkey"; //< access token key
36 char *t_secret = "tsecret"; //< access token secret
45 bh=oauth_body_hash_data(strlen(data), data);
46 uh = (char*) malloc((strlen(url)+strlen(bh)+2) * sizeof(char));
53 printf("URL: %s\n", uh);
54 req_url = oauth_sign_url2(uh, &postarg, OA_HMAC, NULL, c_key, c_secret, t_key, t_secret);
55 printf("POST: %s?%s\n", req_url, postarg);
58 sig_url = malloc(2+strlen(req_url)+strlen(postarg));
59 sprintf(sig_url,"%s?%s",req_url, postarg);
60 reply = oauth_post_data(sig_url, data, strlen(data), "Content-Type: application/json");
61 if(sig_url) free(sig_url);
64 printf("REPLY: %s\n", reply);
67 printf("Error performing the request\n");
72 int main (int argc, char **argv) {
73 char *base_url = "http://localhost/oauthtest.php";
74 char *teststring="Hello World!";
76 /* TEST_BODY_HASH_FILE and TEST_BODY_HASH_DATA are only
77 * here as examples and for testing during development.
79 * the my_data_post() function above uses oauth_body_hash_data()
82 #if defined TEST_BODY_HASH_FILE || defined TEST_BODY_HASH_DATA
86 #ifdef TEST_BODY_HASH_FILE // example hash file
87 char *filename="/tmp/test";
88 bh=oauth_body_hash_file(filename);
89 if (bh) printf("%s\n", bh);
93 #ifdef TEST_BODY_HASH_DATA // example hash data
94 bh=oauth_body_hash_data(strlen(teststring), teststring);
95 if (bh) printf("%s\n", bh);
99 my_data_post(base_url, teststring);