Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft OneDrive API client in Perl can't get access token

I created this simple code in Perl to connect Microsoft OneDrive API and list files and folders. But now I'm stopped on getting access token.

I read the Microsoft's documentation to find out, but I nothing found.

Here is the code:

#!/usr/bin/perl -w
use strict;
use LWP; use LWP::UserAgent;

my $client_id = '...';
my $client_secret = '...';
my $client_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36'; # whatever
my $ua = new LWP::UserAgent;
$ua->->show_progress(1);  # Microsoft use url redirection and I want to see the steps
$ua->agent($client_agent);
$ua->timeout(30);
my $URL = 'https://login.live.com/oauth20_desktop.srf'; # from documentation
my @params = (
    "client_id=".$client_id,
    "scope=onedrive.readonly",
    "response_type=token",
    "redirect_uri=https://login.live.com/oauth20_desktop.srf"
);
my $URLFULL = $URL."?".join("&", @params);
my $res = $ua->get($URLFULL);
if ( $res->is_success ) {
    print $res->request->uri->as_string."\n"; # it should be the url with a valid token
    my $block = $res->as_string;
    print $block; # this is the full response
} else {
    die ($res->as_string."error in loading page");
}

So I send a GET message to the URL and it should be redirect to the URL what contain the access token. But I redirected to the same URL what I called.

How can I get the access token? Or where is the mistake in my code? Or is there any working example?

like image 322
netdjw Avatar asked Jan 20 '16 17:01

netdjw


1 Answers

In the documentation, it says that the URL with the params should be like:

GET https://login.live.com/oauth20_authorize.srf?client_id={client_id}&scope={scope}&response_type=token&redirect_uri={redirect_uri} 

Your $URL parameter seems wrong. $URL should be https://login.live.com/oauth20_authorize.srf and redirect URL is https://login.live.com/oauth20_desktop.srf.

I didn't try the code since I don't want to create and MS account just for this ;)

like image 60
zafatar Avatar answered Sep 23 '22 16:09

zafatar