Search code examples
jsonperllwp

Create Google Team Drive with Perl module LWP::Authen::OAuth2


I'm trying to use Perl with LWP::Authen::OAuth2 to perform google team drive creation. Understand to create google team drive using google Drive API, it requires 1 param to be posted that is requestId and another json body name (reference: https://developers.google.com/drive/api/v3/reference/teamdrives/create)

However, I keep getting the error code 400 and error message saying

The Team Drive name must be provided, not empty, and not entirely whitespace.

which indicate the json body of name is not posted correctly.

Below is my code:

# 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;

Appreciate if someone with Perl knowledge will be able to shade some light.


Solution

  • You are not correctly passing the parameters in your call to ->post:

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

    Move everything starting from Content-Type out of the string:

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

    See also the documentation of LWP::UserAgent on the ->post method.