2017-05-13 12 views
0

GoogleドライブV3 APIを使用してGoogleドライブにファイルを保存しようとしました。次のようにGoogle drive api v3 "code":401、 "message": "ログインが必要"

私のコードは次のとおりです。

<pre><code> 
var startApp = function() { 
     gapi.load('auth2', function(){ 
      // Retrieve the singleton for the GoogleAuth library and set up the client. 
      auth2 = gapi.auth2.init({ 
       discoveryDocs:'https://www.googleapis.com/discovery/v1/apis/drive/v3/rest', 
       client_id: 'xxxxxxxxx.apps.googleusercontent.com', 
       api_key:'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 
       cookiepolicy: 'single_host_origin', 
       scope: 'https://www.googleapis.com/auth/drive.file' 
      }); 
     oauthToken = googleUser.Zi.access_token; 
     }); 
     gapi.client.load('drive', 'v3', null); 
    }; 

function sendfile(formData){ 
     var myPostRequest = new ajaxRequest(); 
     myPostRequest.onreadystatechange = function(){ 
      if (myPostRequest.readyState == 4){ 
      if (myPostRequest.status == 200){ 
       var data = JSON.parse(this.responseText); 
       console.log('file uploaded: ', data) 
      } 
      console.log(''image upload not successful: ', this.responseText); 
      } 
     }; 
     myPostRequest.open('POST', 'https://www.googleapis.com/drive/v3/files?fields=contentHints%2Fthumbnail%2Cid%2CimageMediaMetadata%2CisAppAuthorized%2CmimeType%2CwebContentLink%2CwebViewLink&key=xxxxxxxxxxxxx', true); 
     myPostRequest.send(formData); 
    } 

logInButton.onClick = startApp(); 

私はポストの要求に応じて、この401のメッセージを取得し続ける:

{ 
    "error": { 
    "errors": [ 
    { 
    "domain": "global", 
    "reason": "required", 
    "message": "Login Required", 
    "locationType": "header", 
    "location": "Authorization" 
    } 
    ], 
    "code": 401, 
    "message": "Login Required" 
} 
} 

私は何をしないのですか?アクセストークンに投稿要求を提供する必要がありますか、適切な権限と有効範囲で正しくログインしていないのですか?状態の変化をリッスン

// Listen for sign-in state changes. 
      gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus); 

      // Handle the initial sign-in state. 
      updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get()); 

:あなたのようなコードのビットが欠けているよう

答えて

0

は作業Javascript Quickstart for Driveにあなたのコードを比較すると、それが見えます。とにかく、完全なコード実装のリンクを確認してください。 JSのDrive APIで再生するときに使用します。

関連する問題