Bjorn Behrendt
Bjorn Behrendt

Reputation: 1264

Google Picker oAuth Error

I am working on an add-on: http://scripts.edlisten.com/transfer-ownership

During the setup I use a picker to get a json file, and upload it to properties.

It works fine when ran from the script, but when I publish I get an error. I can't even figure out what my next step is to diagnose the issue. I am using the picker right from the documentation example.

Here is a screenshot of the debug log Debug error screenshot

Here is my picker code:

<script> // Picker

    // IMPORTANT: Replace the value for DEVELOPER_KEY with the API key obtained
    // from the Google Developers Console.
    var DEVELOPER_KEY = 'removed for post to stackoverflow';
    var DIALOG_DIMENSIONS = {width: 800, height: 380};
    var pickerApiLoaded = false;

    /**
     * Loads the Google Picker API.
     */
    function onApiLoad() {
      gapi.load('picker', {'callback': function() {
        pickerApiLoaded = true;
      }});
     }

    /**
     * Gets the user's OAuth 2.0 access token from the server-side script so that
     * it can be passed to Picker. This technique keeps Picker from needing to
     * show its own authorization dialog, but is only possible if the OAuth scope
     * that Picker needs is available in Apps Script. Otherwise, your Picker code
     * will need to declare its own OAuth scopes.
     */
    function getOAuthToken() {
      google.script.run.withSuccessHandler(createPicker)
          .withFailureHandler(showError).getOAuthToken();
    }

    /**
     * Creates a Picker that can access the user's spreadsheets. This function
     * uses advanced options to hide the Picker's left navigation panel and
     * default title bar.
     *
     * @param {string} token An OAuth 2.0 access token that lets Picker access the
     *     file type specified in the addView call.
     */
    function createPicker(token) {

//       var docsView = new google.picker.DocsView().setIncludeFolders(true).setMimeTypes('application/vnd.google-apps.folder').setSelectFolderEnabled(true);
       var docsView = new google.picker.View(google.picker.ViewId.DOCS).setMimeTypes("application/json");
      var uploadView = new google.picker.DocsUploadView();

      if (pickerApiLoaded && token) {
        var picker = new google.picker.PickerBuilder()
            .addView(uploadView)
            .addView(docsView)
            .hideTitleBar()
            .setOAuthToken(token)
            .setDeveloperKey(DEVELOPER_KEY)
            .setCallback(pickerCallback)
            .setOrigin(google.script.host.origin)
            .setSize(DIALOG_DIMENSIONS.width - 2,
                DIALOG_DIMENSIONS.height - 2)
            .build();
        picker.setVisible(true);
      } else {
        showError('Unable to load the file picker.');
      }
    }

    /**
     * A callback function that extracts the chosen document's metadata from the
     * response object. For details on the response object, see
     * https://developers.google.com/picker/docs/result
     *
     * @param {object} data The response object.
     */
    function pickerCallback(data) {
      var action = data[google.picker.Response.ACTION];
      if (action == google.picker.Action.PICKED) {
            $("#uploadSpinner").show();
        var doc = data[google.picker.Response.DOCUMENTS][0];
        var id = doc[google.picker.Document.ID];
//        var url = doc[google.picker.Document.URL];
//        var title = doc[google.picker.Document.NAME];
        google.script.run.withSuccessHandler(showJson).saveKey(id);
      } else if (action == google.picker.Action.CANCEL) {
        document.getElementById('result').innerHTML = 'Picker canceled.';
      }
    }


    /**
     * Displays an error message within the #result element.
     *
     * @param {string} message The error message to display.
     */
    function showError(message) {
    $("#previousSpinner").hide();
      document.getElementById('result').innerHTML = 'Error: ' + message;
    }


    function showJson(resultData){
    $("#uploadSpinner").hide();
 if (resultData.status == "good"){
//       google.script.run.setTrigger("on");
       document.getElementById('client_id').innerHTML = resultData.client_id;
       $('#Step2').click();
} else {
$("#step1result").html(resultData.status);
}

    };

  </script>

Upvotes: 0

Views: 233

Answers (1)

John McGowan
John McGowan

Reputation: 11

Maybe you can try to .setOrigin('https://docs.google.com'). That helped me out.

Upvotes: 0

Related Questions