What could be the reason for attempting to insert metadata into a Google Drive document resulting in it being saved as plain text

I'm working on updating the metadata of a file that was previously uploaded to Google Drive. The metadata includes the title and description. However, I've noticed that when I submit the file, the name remains unchanged. The metadata seems to be added to the content of the text file instead of updating the file's actual title.

Snippet of the code being used:

$.ajax({
  url: "https://www.googleapis.com/upload/drive/v2/files/---?UploadType=multipart",
  data: {
    title: "name...",
    // other parameters specified by Google
  },
  headers: {
    "Authorization": "Bearer ..."
  },
  contentType: "application/json", //as specified by Google
  type: "PUT",
  success: function () {
    // ...
  }
});

Here's how the title appears in the Google text file:

title=name...

Answer №1

It is my understanding that your objective is as stated below.

  • You possess a file within your Google Drive.
  • You aim to modify the filename of the file using Drive API v2 with ajax.
  • Your access token is valid for updating the file's filename.

Considering this scenario, how about implementing the following adjustments?

Points of Modification:

  • To alter the file metadata, utilize the
    PUT https://www.googleapis.com/drive/v2/files/fileId
    endpoint.
  • Ensure to convert the file metadata JSON object into a string using JSON.stringify().

Once these aspects are integrated into your code snippet, it should appear like the following.

Modified script:

$.ajax({
  url: "https://www.googleapis.com/drive/v2/files/{fileId}",
  data: JSON.stringify({title: "name..."}),
  headers: {
    "Authorization": "Bearer ###your access token###",
  },
  contentType: "application/json",
  type: "PUT",
  success: function (data) {
    console.log(data);
  },
  error: function (data) {
    console.log(data);
  }
});
  • If you prefer to utilize Drive API v3, adjust the script as shown below.

      $.ajax({
        url: "https://www.googleapis.com/drive/v3/files/{fileId}",
        data: JSON.stringify({name: "name..."}),
        headers: {
          "Authorization": "Bearer ###your access token###",
        },
        contentType: "application/json",
        type: "PATCH",
        success: function (data) {
          console.log(data);
        },
        error: function (data) {
          console.log(data);
        }
      });
    

References:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Troubleshooting the issue of autoplay not functioning in HTML5 audio

I'm facing a strange issue with my code - the autoplay feature for audio is not working as expected. Typically, whenever I insert this particular piece of code into a website, the music starts playing automatically. However, it seems to be malfunctio ...

Leveraging external JSON data in a Jade template with Node.js

Is there a way to successfully pass a Json Object from XMLHttpRequest to my jade file? I am currently experiencing a blank page display and a 500 internal error being sent by the server for the get method. var express = require('express'); var r ...

How can transitions be activated in Bootstrap 4 carousel?

Having an issue with my Bootstrap 4 carousel where I need to move 4 slides at a time. I have tried using the code below: $(this).carousel(4); However, the problem is that the carousel only jumps to that index without showing any transitions. Using $(this) ...

What is the best way to delete an item from a React array state?

In my Firebase database, I have an array stored under the root branch called Rooms. Within the app, there is a state named rooms which is also an array. I successfully set it up so that when a user enters a new room name and submits it, it gets added to th ...

What is the best way to incorporate external HTML content while ensuring HTML5 compatibility? Exploring the different approaches of using PHP, HTML

While this may seem like a simple task to the experts out there, I have been struggling for over an hour without success... My objective is to use a single footer file and menu file for all my webpages while considering blocking, speed, and other factors. ...

Using ServiceWorker with React and Typescript

If you are working on a javascript-based React project, adding a Service Worker is simply a matter of updating serviceWorker.unregister() to serviceWorker.register() in index.jsx. Here is an example project structure: - src |- index.jsx |- serviceWo ...

Toggle visibility of div content when hovering over a link by leveraging the data attribute, with the div initially visible

I have a collection of links: <p><a href="#" class="floorplan initial" data-id="king"><strong>King</strong></a><br> 4 Bedrooms x 2.5 Bathrooms</p> <p><a href="#" class="floorplan" data-id="wood">< ...

Using Multiline Strings for Arguments

Is there a way to successfully pass multi-line strings containing spaces and tabs as a parameter to an express server? Below is the Express.js code snippet which accepts the parameter: app.get('/:prompt', async (req, res) => { ...

Ensuring that toggleClass is consistently displayed for all visitors to the website

Is there a way to make toggleClass save and remain for all visitors of the site? Although I tried, this method appears to be not working: https://jsfiddle.net/715j94gL/3/ $(function(){ $(".worker").click(function(){ $(this).toggle ...

When the enter key is pressed, the anchor tag does not function properly after setting the tabindex value to "0" for the surrounding div element

Having an issue with Safari not focusing on my anchor element. I added tabindex="0" around the div to address this, but now the link doesn't work when pressing enter in any browser. Am I missing something here? <div class="used__button&quo ...

Executing a javascript function from an ajax-loaded webpage

I have a form where I upload a file using an ajax call that includes an API request. Once the API call is successful, I need to update a table displaying the list of uploaded files. Initially, I attempted calling a JavaScript function within the ajax page, ...

What is the most effective method for obtaining only the "steamid" from an AJAX request (or any other method)?

I have been attempting to extract only the "steamid" from an AJAX link without success. Could someone please provide some assistance? Here is the link to find and retrieve only the "steamid": here This is the code I have tried: var xhttp = new XMLHt ...

Saving an incremented number using Vue JS in Firebase database

I have a VueJS app integrated with Firebase JSON database where I'm trying to implement a feature that allows users to update the vote count of a comment by clicking an arrow (similar to upvotes on this website). The function works properly, but the V ...

Fluctuating Values in Array Distribution

I have a set of users and products that I need to distribute, for example: The number of values in the array can vary each time - it could be one value one time and three values another time. It is important that each user receives a unique product with ...

Disabling JavaScript for a particular page using Selenium:

My current challenge involves navigating to a webpage, locating a link to another page within it, and proceeding to that link without using javascript. One approach I've considered involves the following method: options = Options() options.add_exper ...

The variable you have attempted to access has not been assigned

Querying mongo through mongoose with customer.find() returns the following data: [{ _id: 5029a09e099fb5095fdb2d73, clientId: 22, company: 'X', email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="97cfd7 ...

What steps can be taken to ensure that AngularJS does not detach a form input's value from its model if it is invalid?

While working on a form implementation in AngularJS, I encountered a baffling behavior that has left me puzzled. It seems that whenever I set ng-minlength=5 as an input attribute, AngularJS disconnects the value until it meets the length requirement. Thi ...

Express application failed to deploy due to boot timeout error R10

After researching extensively on Stack Overflow and various other websites, I have been unable to solve a problem with my small Express app using ES6 syntax. Upon deploying it to Heroku, I encountered the following error: LOGS : 2021-04-09T12:14:14.688546 ...

What is the best way to sort a table by column index using HTML?

I find myself in a situation where I am not well-versed in HTML, Javascript, and CSS. Here is the scenario: <div class="table"> <table class="display-table"> <thead> <tr> ...

Navigating AngularJS with multiple external files and folders

Recently dove into Angular and hit a roadblock with routing. I followed the setup instructions, but for some reason it's not functioning as expected. index.html: <!DOCTYPE html> <html lang="en> <head> <meta charset="utf-8> ...