0

Prior Research

Please do not close this question as a duplicate because my question deals with how to resolve the specific error message I am receiving and not the general question of whether my objective is achievable or not — as some other related questions, yielded by my research and below detailed, have asked.

Related questions and why they do not apply here

  1. This question, asked 7/27/2012, does not apply because it: (1) is too old (after 10 months, new solutions/methods might exist) and (2) does not deal with the specific error message I am experiencing.
  2. This question, asked 10/12/2012, fails to apply for similar reasons.
  3. My below code was copied from here which was forked from here. These are presumably, working solutions because they have been referenced as such from other question/answer exchanges here on Stack Overflow.

Objective

Programmatically, I am trying to:

  1. Search my email inbox.
  2. Find Excel (.xls) file attachments.
  3. Upload those .xls file attachments to Google Drive.
  4. While uploading, convert the .xls files into a Google Spreadsheet file format.

Problem

When I execute processInbox() (code shown at the bottom of this question), it fails and I get the error message shown below.

Error Message

Request failed for returned code 403.
Server response:
{
   "error":{
      "errors":[
         {
            "domain":"usageLimits",
            "reason":"accessNotConfigured",
            "message":"AccessNotConfigured"
         }
      ],
      "code":403,
      "message":"AccessNotConfigured"
   }
}
(line 13, file "DriveUpload")

Question

What am I doing wrong? And how can I fix it?

For example, do I need to do something special in my API console relative to setting up my project to, say, access Google Drive or something? What am I missing?

Note: I have not yet successfully implemented oAuth in any of my applications, yet.

Error Source

Line 13

(This is the code line referenced by the error message.)

var uploadRequest = UrlFetchApp.fetch("https://www.googleapis.com/upload/drive/v2/files/?uploadType=media&convert=true&key="+key, params); // convert=true convert xls to google spreadsheet

Code

The complete body of code I am working with is shown below for your reference. I extracted the error-triggering, “line 13,” and highlighted it above to help us focus on the proximate cause of the problem.

DriveUpload.js

function uploadXls(file) {
  authorize();
  var key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // <-- developer key
  var metadata = { title: file.getName() }
  var params = {method:"post",
                oAuthServiceName: "drive",
                oAuthUseToken: "always",
                contentType: "application/vnd.ms-excel",
                contentLength: file.getBytes().length,
                payload: file.getBytes()
               };
  // convert=true convert xls to google spreadsheet
  var uploadRequest = UrlFetchApp.fetch("https://www.googleapis.com/upload/drive/v2/files/?uploadType=media&convert=true&key="+key, params);
  var uploadResponse = Utilities.jsonParse(uploadRequest.getContentText());
  var params = {method:"put",
                oAuthServiceName: "drive",
                oAuthUseToken: "always",
                contentType: "application/json",
                payload: Utilities.jsonStringify(metadata)
               };
  var metaRequest = UrlFetchApp.fetch("https://www.googleapis.com/drive/v2/files/"+uploadResponse.id+"?key="+key, params)
  return DocsList.getFileById(uploadResponse.id);
}

function authorize() {
  var oauthConfig = UrlFetchApp.addOAuthService("drive");
  var scope = "https://www.googleapis.com/auth/drive";
  oauthConfig.setConsumerKey("anonymous");
  oauthConfig.setConsumerSecret("anonymous");
  oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
  oauthConfig.setAuthorizationUrl("https://accounts.google.com/OAuthAuthorizeToken");    
  oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");  
}

function processInbox() {
  // get all threads in inbox
  var threads = GmailApp.getInboxThreads();
  for (var i = 0; i < threads.length; i++) {
    // get all messages in a given thread
    var messages = threads[i].getMessages();
    // iterate over each message
    for (var j = 0; j < messages.length; j++) {
      // log message subject
      var subject = messages[j].getSubject()
      //Logger.log(subject);
      if ( subject == "with xls attach" ){
        Logger.log(messages[j].getSubject());
        var attach = messages[j].getAttachments()[0];
        var name = attach.getName();
        var type = attach.getContentType();
        //var data = attach.getDataAsString();
        Logger.log( name + " " + type + " " );
        var file = uploadXls(attach);
        SpreadsheetApp.open(file);
      }
    }
  }
};
Community
  • 1
  • 1
Let Me Tink About It
  • 15,156
  • 21
  • 98
  • 207

2 Answers2

1

Drive API is already built in GAS: https://developers.google.com/apps-script/reference/drive/

Use DriveApp and your problems go away ;-)

Taras
  • 1,023
  • 8
  • 17
  • How did you get the file to convert using DriveApp? Was it with `getAs(contentType)` on the file? – Fred May 29 '13 at 12:15
0

This maybe a temp solution

Step 1: Use a Google Form to Collect Data to a Google spreadsheet

Step 2: Add the Zoho Sheet App to your Google Drive

In a Zoho Sheet Goto Data Menu »Link External Data Select either CSV RSS/Atom Feed or HTML Page

You can schedule it to update at specific time intervals

What I like is the VBA and Macros in Zoho You can also do Pivot Charts and Tables

You can copy and paste Excel VBA into Zoho ! I have an Unpivot VBA that I will run on my Tabular dataset before I can do a PivotChart

It is hard to beat all the functionality of Excel and I often fall back on familiar tools ! If I hear of anything I will post it

Good luck