0

I have been trying to attach Google Drive Document to my php gmail api compose box.

So far I have managed to get file id on select file but can't find a proper way to how to attach that file.download url to mail box . is there any way to do so?

i also try download file using following code but showing error "Only binary file can be downloaded"

 var downloadUrl = 'https://www.googleapis.com/drive/v2/files/' + file.id + '?alt=media';
    //var downloadUrl2 = file.downloadUrl1;
  if (downloadUrl) {
    //var accessToken = gapi.auth.getToken().access_token;
    //debugger;
    var xhr = new XMLHttpRequest();
    xhr.open('GET',downloadUrl);
    debugger;
    xhr.setRequestHeader('Authorization', 'Bearer ' + AUTH_TOKEN);
    xhr.onload = function() {
     alert(xhr.responseText);

    };
    xhr.onerror = function() {
     alert('Error');
    };
    xhr.send();
  } else {
    alert('No Url');
  }

1 Answers1

0

I also try to download file using following code but showing error "Only binary file can be downloaded

This means you need to convert it to binary first. I suggest you look into using:

  1. WindowBase64.btoa() - method creates a base-64 encoded ASCII string from a String object in which each character in the string is treated as a byte of binary data. var encodedData = window.btoa(stringToEncode);

  2. arraybuffer & Uint8Array - This example reads an image as a binary file and creates an 8-bit unsigned integer array from the raw bytes oReq.onload = function (oEvent) { var arrayBuffer = oReq.response; // Note: not oReq.responseText if (arrayBuffer) { var byteArray = new Uint8Array(arrayBuffer); for (var i = 0; i < byteArray.byteLength; i++) { // do something with each byte in the array } } }

Attach file from Google Drive to gmail api compose mail

From ctrlq tutorial Begin by creating a MIME message that complies with RFC 2822 standard and call the Gmail API to sends the specified message to the recipients in the To, Cc, and Bcc headers. We use the /upload URI with the messages/send method for uploading the files with the message and the uploadType is set to media for uploading the files without any metadata.

Additonal Reading: GMAIL API for sending Email with attachment.

Community
  • 1
  • 1
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56