dqwh1209 2016-07-05 08:42
浏览 67

将文件从Google云端硬盘附加到gmail api撰写邮件

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条回答 默认 最新

  • dongli2000 2016-07-15 08:46
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line