2016-01-20 84 views
7

我在Perl中创建了此简单代码,用于连接Microsoft OneDrive API和列表文件和文件夹。但现在我停止了获取访问令牌。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"); 
} 

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

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

+0

如果你只是贴,它看起来像你有一个错字:'“的client_id =” $。 clien_tid,' – bolav

+0

谢谢你,修正 – netdjw

+0

对于这个特性,如果你在做任何请求,我认为明智的做法是在把它放到你的代码之前用mozila上的REST进行测试。 – robel

回答

1

在本文档中,它说,与PARAMS的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解释只是为了这一点;)

相关问题