net*_*djw 7 perl oauth-2.0 onedrive
我创建了这个简单的代码Perl来连接Microsoft OneDrive API和列表文件和文件夹.但现在我停止获取访问令牌.
我阅读了微软的文档以找出答案,但我一无所获.
这是代码:
#!/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");
}
Run Code Online (Sandbox Code Playgroud)
所以我向GETURL 发送一条消息,它应该重定向到包含访问令牌的URL.但我重定向到我所谓的相同URL.
我怎样才能获得访问令牌?或者我的代码中的错误在哪里?或者有任何工作示例吗?
小智 1
在文档中,它说带有参数的 URL 应该是这样的:
GET https://login.live.com/oauth20_authorize.srf?client_id={client_id}&scope={scope}&response_type=token&redirect_uri={redirect_uri}
Run Code Online (Sandbox Code Playgroud)
你的$URL参数好像不对。$URL应该是https://login.live.com/oauth20_authorize.srf且重定向 URL 是https://login.live.com/oauth20_desktop.srf。
我没有尝试该代码,因为我不想为此创建 MS 帐户;)