【问题标题】:Create Google Team Drive with Perl module LWP::Authen::OAuth2使用 Perl 模块 LWP::Authen::OAuth2 创建 Google Team Drive
【发布时间】:2018-11-12 08:40:26
【问题描述】:

我正在尝试将 Perl 与 LWP::Authen::OAuth2 一起使用来执行 google 团队驱动器创建。了解使用 google Drive API 创建 google team drive,它需要发布 1 个参数,即 requestId 和另一个 json 正文 name(参考:https://developers.google.com/drive/api/v3/reference/teamdrives/create

但是,我不断收到错误代码 400 和错误消息说

必须提供团队云端硬盘名称,不能为空,也不能全是空格。

这表明name的json正文没有正确发布。

下面是我的代码:

# Go get the auth tokens
$oauth2->request_tokens(code => $code);

my $requestID = "randomrequestID";
my $json = '{"name": "anyteamdrivename"}';

my $resp = $oauth2->post("https://www.googleapis.com/drive/v3/teamdrives?requestId=$requestID, Content-Type => application/json, Content => $json");


my $data = decode_json($resp->content());
use Data::Dumper;
print Dumper $data;

感谢有 Perl 知识的人是否能够遮蔽一些光线。

【问题讨论】:

  • 您没有正确传递参数:"https://www.googleapis.com/drive/v3/teamdrives?requestId=$requestID, Content-Type => application/json, Content => $json" - 将所有从 Content-Type 开始的内容移出字符串。
  • 我只是将其更改为my $resp = $oauth2->post("https://www.googleapis.com/drive/v3/teamdrives?requestId=$requestID", "Content-Type => application/json", "Content => $json");,但我仍然收到相同的错误代码和消息
  • 是的,因为您仍然传递错误的参数。请参阅 ->post: metacpan.org/pod/LWP::UserAgent 上的 LWP::UserAgent 的文档。你需要'Content-Type' => 'application/json', 'Content' => $json)
  • 哦,天哪,你是对的 Co​​rion。你这样一个简单而准确的答案。我刚刚纠正了它们,它可以工作,谢谢 Corion。

标签: json perl lwp


【解决方案1】:

您没有正确地将调用中的参数传递给->post

my $resp = $oauth2->post("https://www.googleapis.com/drive/v3/teamdrives?requestId=$requestID, Content-Type => application/json, Content => $json");

将从 Content-Type 开始的所有内容移出字符串:

my $resp = $oauth2->post(
    "https://www.googleapis.com/drive/v3/teamdrives?requestId=$requestID",
    "Content-Type" => "application/json",
    "Content" => $json
);

另请参阅LWP::UserAgent 的有关->post 方法的文档。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 2012-01-09
    • 1970-01-01
    • 1970-01-01
    • 2015-04-22
    • 2016-06-30
    • 2016-09-07
    相关资源
    最近更新 更多