Can Google Drive App Rename a Copied File?

This topic was automatically generated from Slack. You can find the original thread here.

I see the Google Drive App can make a copy of a file. Is there a way to RENAME this file?

Yes, you can rename a file in Google Drive using the Google Drive API. To do this, you’ll need to make a PATCH request to the following endpoint: https://www.googleapis.com/drive/v3/files/{fileId} Replace {fileId} with the ID of the file you want to rename. You’ll also need to pass the new name in the request body as a JSON object: on { "name": "NewFileName" } Here’s an example Pipedream component that renames a file in Google Drive: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { fileId: { type: "string", label: "File ID", }, newName: { type: "string", label: "New File Name", }, }, async run({ steps, $ }) { const url = `https://www.googleapis.com/drive/v3/files/${this.fileId}`; const body = { name: this.newName, }; const headers = { Authorization: `Bearer ${this.google.$auth.oauth_access_token}`, "Content-Type": "application/json", }; return await axios($, { method: "PATCH", url: url, data: body, headers: headers, }); }, }); This component accepts the fileId and newName as input props. Make sure to connect your Google account in the Pipedream UI when using this component.

how do I make a PATCH request?

To make a PATCH request using Pipedream, you can use the axios function from the @pipedream/platform package. Here’s an example of a Pipedream component that makes a PATCH request: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { url: { type: "string", label: "URL to make request to", }, data: { type: "object", label: "Data to send in the request body", }, }, async run({ steps, $ }) { const headers = { Authorization: `Bearer ${this.google.$auth.oauth_access_token}`, "Content-Type": "application/json", }; return await axios($, { method: "PATCH", url: this.url, data: this.data, headers: headers, }); }, }); This component accepts the url and data as input props. Make sure to connect your Google account in the Pipedream UI when using this component, and replace the Authorization header with the appropriate one for the API you’re working with.

You should be able to use the Google Drive action “Update File” to update the name of the file.

that’s a good nuance — the question was about “rename”, so Pi didn’t suggest the “Update” action

yeah this one is hard given the way Pi works but definitely a good callout

would UPDATE allow me to rename the file?

never mind. I see it.

Thanks!

is there a way to change the permissions of the file?

Yes, you can change the permissions of a file in Google Drive using the Google Drive API. To do this, you’ll need to create a new permission for the file using the following endpoint: https://www.googleapis.com/drive/v3/files/{fileId}/permissions. Replace {fileId} with the ID of the file you want to change permissions for. Here’s an example Pipedream component that changes the permissions of a file in Google Drive: import { axios } from "@pipedream/platform"; export default defineComponent({ props: { fileId: { type: "string", label: "File ID", }, role: { type: "string", label: "Role", }, type: { type: "string", label: "Type", }, }, async run({ steps, $ }) { const url = `https://www.googleapis.com/drive/v3/files/${this.fileId}/permissions`; const body = { role: this.role, type: this.type, }; const headers = { Authorization: `Bearer ${this.google.$auth.oauth_access_token}`, "Content-Type": "application/json", }; return await axios($, { method: "POST", url: url, data: body, headers: headers, }); }, }); This component accepts the fileId, role, and type as input props. The role can be “owner”, “writer”, “reader”, or “commenter”, and the type can be “user”, “group”, “domain”, or “anyone”. Make sure to connect your Google account in the Pipedream UI when using this component.

I am getting the following error.

TypeError
Cannot read property ‘$auth’ of undefined

I apologize for the confusion in my previous response. It seems I made a mistake in the example code. Please update the Authorization header in the example code like this: ```
const headers = { Authorization: Bearer ${$.auth.oauth_access_token}, “Content-Type”: “application/json”, };

Now I am getting this error:

Error - Request failed with status code 401
{“error”:{“code”:401,“message”:“Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",“errors”:[{“message”:"Invalid Credentials”,“domain”:“global”,“reason”:“authError”,“location”:“Authorization”,“locationType”:“header”}],“status”:“UNAUTHENTICATED”}}
DETAILS

steps.code{1}
debug{5}

update_task

CONFIGURE

RESULTS

I apologize for the confusion earlier. It seems there’s an issue with the authentication credentials. To fix the error, please make sure you’ve connected your Google account in the Pipedream UI when using the component. Also, ensure that the Authorization header is set correctly in the axios request: const headers = { Authorization: `Bearer ${$.auth.oauth_access_token}`, "Content-Type": "application/json", }; If you’ve already connected your Google account and set the Authorization header correctly but still encounter the error, it’s possible that the access token has expired or the required scopes are missing. In that case, please try reconnecting your Google account in the Pipedream UI and ensure that the necessary OAuth scopes are granted.

you mean connect my Google Account prior to running this step?

Yes, that’s correct. You need to connect your Google Account in the Pipedream UI before running the step that uses the Google API. When you add the component to your workflow, you’ll see an option to connect your Google Account. Once connected, the component will have access to the necessary OAuth access token to authenticate your requests.

This is not working.

Do you have anything additional suggestions?

I apologize for the issues you’ve been facing. Let’s try to troubleshoot the problem together. Can you please provide more details about the error you’re encountering or the specific step in the workflow that’s causing the issue? This will help me better understand the problem and offer a more accurate solution.

I have my trigger which is fine.

Then I run the GOOGLE DRIVE APP.

I select USE ANY GOOGLE DRIVE API IN PYTHON.

I remove the old code and put in the new code that you gave me.