summaryrefslogtreecommitdiffabout
path: root/test/test-oauth-consumer.cc
blob: b3ddef5124f53b37d86b0c35ac594bb9686b3b0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <cassert>
#include <stdexcept>
using namespace std;
#include <openssl/sha.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <opkele/exception.h>
#include <opkele/debug.h>
#include <opkele/util.h>
#include <opkele/util-internal.h>
#include <opkele/curl.h>
#include <opkele/oauth/consumer.h>

ostream& operator<<(ostream& o,const opkele::oauth::token_t& t) {
    o << "{ key: \"" << t.key << "\", secret: \"" << t.secret <<"\" }";
    return o;
}

int main(int,char**) {
    try {
	opkele::oauth::simple_consumer sc(
		opkele::oauth::simple_provider_endpoints(
		    "http://term.ie/oauth/example/request_token.php",
		    "http://term.ie/oauth/example/user_authorization.php",
		    "http://term.ie/oauth/example/access_token.php",
		    "HMAC-SHA1", opkele::oauth::oauth_post_body,
		    opkele::oauth::oauth_auth_header),
		opkele::oauth::token_t( "key","secret" ) );
	opkele::oauth::token_t rt = sc.get_request_token();
	cout << "Request token: " << rt << endl;
	cout << "Authorize URL: " << sc.get_authorize_url(rt) << endl;
	opkele::oauth::token_t at = sc.get_access_token(rt);
	cout << "Access token: " << at << endl;

	opkele::fields_t test;
	test.set_field("foo","bar");
	opkele::util::curl_pick_t curl = opkele::util::curl_t::easy_init();
	opkele::oauth::http_request_t hr("POST",
		"http://term.ie/oauth/example/echo_api.php");
	CURLcode r = curl.misc_sets();
	r || (r=curl.set_write());
	if(r)
	    throw opkele::exception_curl(OPKELE_CP_ "failed to set curly options",r);
	sc.prepare_request(hr,opkele::fields_t(),test,&at).setup_curl(curl);
	if( (r=curl.easy_perform()) )
	    throw opkele::exception_curl(OPKELE_CP_ "failed to perform curly request",r);
	cout << "Response: " << curl.response << endl;

#ifdef OPKELE_HAVE_KONFORKA
    }catch(konforka::exception& e) {
	cerr
	    << "oops, caught " << opkele::util::abi_demangle(typeid(e).name()) << endl
	    << " what: " << e.what() << endl
	    << " where: " << e.where() << endl;
	if(!e._seen.empty()) {
	    cerr << " seen:" << endl;
	    for(list<konforka::code_point>::const_iterator
		    i=e._seen.begin();i!=e._seen.end();++i) {
		cerr << "  " << i->c_str() << endl;
	    }
	}
#endif
    }catch(std::exception& e){
	cerr
	    << "oops, caught " << opkele::util::abi_demangle(typeid(e).name()) << endl
	    << " what: " << e.what() << endl;
    }
}