Google云端分段上传文件返回错误403(确定)

时间:2015-12-03 08:11:26

标签: javascript google-drive-api

我正在尝试使用ClientID将文件上传到驱动器。我收到以下错误说403(好)。

调用POST here 403(确定)

时出错

我查看了Google的文档,但我仍然没有找到解决方案

这是我的总代码。

<html> <head>
<script type="text/javascript">     
    var CLIENT_ID = <CLIENT_ID>;

    var SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly'];

    /**
     * Check if current user has authorized this application.
     */
    function checkAuth() {
        gapi.auth.authorize(
                {
                    'client_id': CLIENT_ID,
                    'scope': SCOPES.join(' '),
                    'immediate': true
                }, handleAuthResult);
    }

    /**
     * Handle response from authorization server.
     *
     * @param {Object} authResult Authorization result.
     */
    function handleAuthResult(authResult) {
        var authorizeDiv = document.getElementById('authorize-div');
        if (authResult && !authResult.error) {
            // Hide auth UI, then load client library.
            authorizeDiv.style.display = 'none';
            loadDriveApi();
        } else {
            // Show auth UI, allowing the user to initiate authorization by
            // clicking authorize button.
            authorizeDiv.style.display = 'inline';
        }
    }

    /**
     * Initiate auth flow in response to user clicking authorize button.
     *
     * @param {Event} event Button click event.
     */
    function handleAuthClick(event) {
        gapi.auth.authorize(
                {client_id: CLIENT_ID, scope: SCOPES, immediate: false},
                handleAuthResult);
        return false;
    }

    /**
     * Load Drive API client library.
     */
    function loadDriveApi() {
        gapi.client.load('drive', 'v2', insertFile(callback));
    }


     function callback(){
        console.log("It works.");
     }

    /**
     * Insert new file.
     *
     * @param {File} fileData File object to read data from.
     * @param {Function} callback Function to call when the request is complete.
     */
           function insertFile(callback) {
    const boundary = '-------314159265358979323846';
    const delimiter = "\r\n--" + boundary + "\r\n";
    const close_delim = "\r\n--" + boundary + "--";

    var xhr = new XMLHttpRequest();
    xhr.responseType = 'blob';
    xhr.onload = function() {
        var reader  = new FileReader();
        reader.onloadend = function () {
         var  fileData =reader.result;
            var contentType = "image/jpg";
            var metadata = {
                'title': "InstaImage.jpg",
                'mimeType': contentType
            };

            var base64Data = btoa(fileData);
            var multipartRequestBody =
                    delimiter +
                    'Content-Type: application/json\r\n\r\n' +
                    JSON.stringify(metadata) +
                    delimiter +
                    'Content-Type: ' + contentType + '\r\n' +
                    'Content-Transfer-Encoding: base64\r\n' +
                    '\r\n' +
                    base64Data +
                    close_delim;

            var request = gapi.client.request({
                'path': '/upload/drive/v2/files',
                'method': 'POST',
                'params': {'uploadType': 'multipart'},
                'headers': {
                    'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
                },
                'body': multipartRequestBody});
            if (!callback) {
                callback = function(file) {
                    console.log(file)
                };
            }
            request.execute(callback);
            console.log(fileData);
        };
        reader.readAsDataURL(xhr.response);
    };
    xhr.open('GET', "https://scontent.cdninstagram.com/hphotos-xpt1/t51.2885-15/e15/11324950_950675781667048_1239101466_n.jpg");
    xhr.send();

    /**
     * Append a pre element to the body containing the given message
     * as its text node.
     *
     * @param {string} message Text to be placed in pre element.
     */
    function appendPre(message) {
        var pre = document.getElementById('output');
        var textContent = document.createTextNode(message + '\n');
        pre.appendChild(textContent);
    }

</script>
<script src="https://apis.google.com/js/client.js?onload=checkAuth">
</script> </head> <body> <div id="authorize-div" style="display: none">
<span>Authorize access to Drive API</span>
<!--Button for the user to click to initiate auth sequence -->
<button id="authorize-button" onclick="handleAuthClick(event)">
    Authorize
</button> </div> <pre id="output"></pre> </body> </html>

我的代码有问题吗。

0 个答案:

没有答案