1package WebService::Dropbox::TokenFromOAuth1;
2use strict;
3use warnings;
4use JSON;
5use Net::OAuth;
6use LWP::UserAgent;
7
8sub token_from_oauth1 {
9    my ($class, $args) = @_;
10    my $request = Net::OAuth->request('protected resource')->new(
11        consumer_key => $args->{consumer_key},
12        consumer_secret => $args->{consumer_secret},
13        request_url => 'https://api.dropboxapi.com/1/oauth2/token_from_oauth1',
14        request_method => 'POST',
15        signature_method => 'PLAINTEXT', # HMAC-SHA1 can't delete %20.txt bug...
16        timestamp => time,
17        nonce => &nonce,
18        token => $args->{access_token},
19        token_secret => $args->{access_secret},
20    );
21    $request->sign;
22    my $ua = LWP::UserAgent->new;
23    my $res = $ua->post($request->to_url);
24    if ($res->is_success) {
25        my $data = decode_json($res->decoded_content);
26        return $data->{access_token};
27    }
28    warn $res->decoded_content;
29    return;
30}
31
32sub nonce {
33    my $length = 16;
34    my @chars = ( 'A'..'Z', 'a'..'z', '0'..'9' );
35    my $ret;
36    for (1..$length) {
37        $ret .= $chars[int rand @chars];
38    }
39    return $ret;
40}
41
421;
43