【发布时间】: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)。 -
哦,天哪,你是对的 Corion。你这样一个简单而准确的答案。我刚刚纠正了它们,它可以工作,谢谢 Corion。