Byron / google-apis-rs

A binding and CLI generator for all Google APIs
http://byron.github.io/google-apis-rs
Other
983 stars 132 forks source link

How to upload file to Team Drive #453

Closed bverhagen closed 8 months ago

bverhagen commented 8 months ago

I am using your google-drive3 package to upload a test CSV file to a team drive. However, my request fails with the following API error:

BadRequest(Object {"error": Object {"code": Number(403), "errors": Array [Object {"domain": String("global"), "message": String("Sharing restrictions cannot be set on a shared drive item."), "reason": String("teamDrivesSharingRestrictionNotAllowed")}], "message": String("Sharing restrictions cannot be set on a shared drive item.")}})

I am using a service account to upload to it. Sharing and using a non-team drive with this service account results in a successful upload.

I have been able to get past most of other API errors for Team Drives by setting the relevant settings to None. However, I am not able to find the relevant setting for this one.

I am currently using the following for my requests:

    let hub = get_hub(&config).await?;

    let mut req = File::default();
    req.content_restrictions = None;
    req.description = Some("Test".to_string());
    req.has_augmented_permissions = Some(false);
    req.parents = Some(vec![target_folder_id]);
    req.name = Some("test.csv".to_string());
    req.owned_by_me = None;
    req.owners = None;
    req.permissions = None;
    req.shared = None;
    req.sharing_user = None;
    req.writers_can_share = None;

    info!("Uploading text data...");
    let result = hub.files().create(req)
        .use_content_as_indexable_text(true)
        .supports_all_drives(true)
        .keep_revision_forever(false)
        .include_permissions_for_view("published")
        .include_labels("ea")
        .ignore_default_visibility(true)
        .upload(std::io::Cursor::new(data), "application/octet-stream".parse().unwrap())
        .await;

    match result {
        Err(e) => match e {
            // The Error enum provides details about what exactly happened.
            // You can also just use its `Debug`, `Display` or `Error` traits
             Error::HttpError(_)
            |Error::Io(_)
            |Error::MissingAPIKey
            |Error::MissingToken(_)
            |Error::Cancelled
            |Error::UploadSizeLimitExceeded(_, _)
            |Error::Failure(_)
            |Error::BadRequest(_)
            |Error::FieldClash(_)
            |Error::JsonDecodeError(_, _) => {
                error!("Request returned an error: {:?}", e);

                return Err(async_graphql::Error {
                    message: format!("Error: {:?}!", e),
                    source: None,
                    extensions: None,
                });
            }
        },
        Ok(_) => Ok(()),

Changing the target_folder_id variable from a shared team drive folder id to a shared non-team drive folder id results in a succesful upload, while it does not for the team drive folder due to the above error.

Going through the documentation, I think I have to somehow end up with setting drive permissions to the TeamDriveRestrictions struct, but I was not able to see how this is set from the above code.

Could you point me to a working example for this or to the right settings for doing this?