2 * @brief self-test and example code for liboauth
4 * @author Robin Gareus <robin@gareus.org>
6 * Copyright 2007, 2008, 2010 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
33 * a example requesting and parsing a request-token from an OAuth service-provider
34 * excercising the oauth-HTTP GET function. - it is almost the same as
35 * \ref request_token_example_post below.
37 void request_token_example_get(void) {
39 const char *request_token_uri = "http://oauth-sandbox.mediamatic.nl/module/OAuth/request_token";
40 const char *req_c_key = "17b09ea4c9a4121145936f0d7d8daa28047583796"; //< consumer key
41 const char *req_c_secret = "942295b08ffce77b399419ee96ac65be"; //< consumer secret
43 const char *request_token_uri = "http://term.ie/oauth/example/request_token.php";
44 const char *req_c_key = "key"; //< consumer key
45 const char *req_c_secret = "secret"; //< consumer secret
47 char *res_t_key = NULL; //< replied key
48 char *res_t_secret = NULL; //< replied secret
53 req_url = oauth_sign_url2(request_token_uri, NULL, OA_HMAC, NULL, req_c_key, req_c_secret, NULL, NULL);
55 printf("request URL:%s\n\n", req_url);
56 reply = oauth_http_get(req_url,NULL);
58 printf("HTTP request for an oauth request-token failed.\n");
60 // parse reply - example:
61 //"oauth_token=2a71d1c73d2771b00f13ca0acb9836a10477d3c56&oauth_token_secret=a1b5c00c1f3e23fb314a0aa22e990266"
65 printf("HTTP-reply: %s\n", reply);
66 rc = oauth_split_url_parameters(reply, &rv);
67 qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
69 && !strncmp(rv[0],"oauth_token=",11)
70 && !strncmp(rv[1],"oauth_token_secret=",18) ){
71 res_t_key=strdup(&(rv[0][12]));
72 res_t_secret=strdup(&(rv[1][19]));
73 printf("key: '%s'\nsecret: '%s'\n",res_t_key, res_t_secret);
78 if(req_url) free(req_url);
79 if(reply) free(reply);
80 if(res_t_key) free(res_t_key);
81 if(res_t_secret) free(res_t_secret);
85 * a example requesting and parsing a request-token from an OAuth service-provider
86 * using the oauth-HTTP POST function.
88 void request_token_example_post(void) {
90 const char *request_token_uri = "http://oauth-sandbox.mediamatic.nl/module/OAuth/request_token";
91 const char *req_c_key = "17b09ea4c9a4121145936f0d7d8daa28047583796"; //< consumer key
92 const char *req_c_secret = "942295b08ffce77b399419ee96ac65be"; //< consumer secret
94 const char *request_token_uri = "http://term.ie/oauth/example/request_token.php";
95 const char *req_c_key = "key"; //< consumer key
96 const char *req_c_secret = "secret"; //< consumer secret
98 char *res_t_key = NULL; //< replied key
99 char *res_t_secret = NULL; //< replied secret
101 char *postarg = NULL;
105 req_url = oauth_sign_url2(request_token_uri, &postarg, OA_HMAC, NULL, req_c_key, req_c_secret, NULL, NULL);
107 printf("request URL:%s\n\n", req_url);
108 reply = oauth_http_post(req_url,postarg);
110 printf("HTTP request for an oauth request-token failed.\n");
112 //parse reply - example:
113 //"oauth_token=2a71d1c73d2771b00f13ca0acb9836a10477d3c56&oauth_token_secret=a1b5c00c1f3e23fb314a0aa22e990266"
116 printf("HTTP-reply: %s\n", reply);
117 rc = oauth_split_url_parameters(reply, &rv);
118 qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
120 && !strncmp(rv[0],"oauth_token=",11)
121 && !strncmp(rv[1],"oauth_token_secret=",18) ){
122 res_t_key=strdup(&(rv[0][12]));
123 res_t_secret=strdup(&(rv[1][19]));
124 printf("key: '%s'\nsecret: '%s'\n",res_t_key, res_t_secret);
129 if(req_url) free(req_url);
130 if(postarg) free(postarg);
131 if(reply) free(reply);
132 if(res_t_key) free(res_t_key);
133 if(res_t_secret) free(res_t_secret);
138 * Main Test and Example Code.
141 * gcc -lssl -loauth -o oauthtest oauthtest.c
143 int main (int argc, char **argv) {
146 const char *url = "http://base.url/&just=append?post=or_get_parameters"
147 "&arguments=will_be_formatted_automatically?&dont_care"
150 const char *c_key = "1234567890abcdef1234567890abcdef123456789";
152 const char *c_secret = "01230123012301230123012301230123";
154 const char *t_key = "0987654321fedcba0987654321fedcba098765432";
156 const char *t_secret = "66666666666666666666666666666666";
159 #if 1 // example sign GET request and print the signed request URL
162 geturl = oauth_sign_url2(url, NULL, OA_HMAC, NULL, c_key, c_secret, t_key, t_secret);
163 printf("GET: URL:%s\n\n", geturl);
164 if(geturl) free(geturl);
168 #if 1 // sign POST ;) example
170 char *postargs = NULL, *post = NULL;
171 post = oauth_sign_url2(url, &postargs, OA_HMAC, NULL, c_key, c_secret, t_key, t_secret);
172 printf("POST: URL:%s\n PARAM:%s\n\n", post, postargs);
174 if(postargs) free(postargs);
178 printf(" *** sending HTTP request *** \n\n");
180 // These two will perform a HTTP request, requesting an access token.
181 // it's intended both as test (verify signature)
183 #if 1 // POST a request-token request
184 request_token_example_post();
186 #if 1 // GET a request-token
187 request_token_example_get();