Perl中的Microsoft OneDrive API客户端无法获取访问令牌

时间:2016-01-20 17:00:09

标签: perl oauth-2.0 onedrive

我在Perl中创建了这个简单的代码,以连接Microsoft OneDrive API和列表文件和文件夹。但现在我停止获取访问令牌。

我读了Microsoft's documentation以找出答案,但我一无所获。

以下是代码:

#!/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");
}

所以我向URL发送了一条GET消息,它应该重定向到包含访问令牌的URL。但我重定向到我所说的相同网址。

如何获取访问令牌?或者我的代码中的错误在哪里?或者有任何工作实例吗?

1 个答案:

答案 0 :(得分:1)

在文档中,它表示带有参数的URL应该是:

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

您的$URL参数似乎有误。 $URL应为https://login.live.com/oauth20_authorize.srf,重定向网址为https://login.live.com/oauth20_desktop.srf

我没有尝试代码,因为我不想为此创建和MS帐户;)