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

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

我在 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 。但我重定向到我调用的相同 URL。

如何获取访问令牌?或者我的代码哪里出错了?或者有任何工作示例吗?

在文档中,它说带有参数的 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 并且重定向 URL 是 https://login.live.com/oauth20_desktop.srf.

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