質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Monaca

「Monaca」はiOS、Android、Windows向けのアプリ開発に対応した、Cordovaベースのモバイルアプリ開発プラットフォームです。HTML5、JavaScriptといったWeb標準技術を用いてモバイルアプリ開発を行うことができます。

Q&A

0回答

2790閲覧

MonacaとGoogle Drive APIとの連携方法

akadashi

総合スコア19

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

Monaca

「Monaca」はiOS、Android、Windows向けのアプリ開発に対応した、Cordovaベースのモバイルアプリ開発プラットフォームです。HTML5、JavaScriptといったWeb標準技術を用いてモバイルアプリ開発を行うことができます。

0グッド

0クリップ

投稿2016/08/08 14:34

###前提・実現したいこと

MonacaとGoogle Drive APIとを連携して、Monacaデバッガーから所定のGoogle Driveへファイルを生成しようと考えています。ところが、そもそも認証がうまく行われないようで、なにも表示されません。
MonacaからこのAPIを利用したいとき、どのような設定で「OAuth 2.0 クライアント ID」を生成する必要があるのでしょうか。

なお、DriveAPIはすでに「有効」に設定済みです。

###該当のソースコード

ソースコードはここから引用しました。
ソースコード挿入場所は、<body>タグの間です。
ソースコード変更箇所は★★★★★★★★★★★★の部分のみです。
★★★★★★★★★★★★の部分は、以下のクライアントIDをGoogle API Consoleで生成し、すべて試しましたが、解決しませんでした。

javascript

1<script type="text/javascript"> 2 var CLIENT_ID = '★★★★★★★★★★★★'; 3 var SCOPES = 'https://www.googleapis.com/auth/drive'; 4 5 /** 6 * Called when the client library is loaded to start the auth flow. 7 */ 8 function handleClientLoad() { 9 window.setTimeout(checkAuth, 1); 10 } 11 12 /** 13 * Check if the current user has authorized the application. 14 */ 15 function checkAuth() { 16 gapi.auth.authorize({ 17 'client_id': CLIENT_ID, 18 'scope': SCOPES, 19 'immediate': true 20 }, 21 handleAuthResult); 22 } 23 24 /** 25 * Called when authorization server replies. 26 * 27 * @param {Object} authResult Authorization result. 28 */ 29 function handleAuthResult(authResult) { 30 var authButton = document.getElementById('authorizeButton'); 31 var main = document.getElementById('main'); 32 authButton.style.display = 'none'; 33 main.style.display = 'none'; 34 if(authResult && !authResult.error) { 35 // Access token has been successfully retrieved, requests can be sent to the API. 36 main.style.display = 'block'; 37 var btn = document.getElementById('saveBtn'); 38 btn.onclick = writeFile; 39 //main.onchange = uploadFile; 40 } else { 41 // No access token could be retrieved, show the button to start the authorization flow. 42 authButton.style.display = 'block'; 43 authButton.onclick = function () { 44 gapi.auth.authorize({ 45 'client_id': CLIENT_ID, 46 'scope': SCOPES, 47 'immediate': false 48 }, 49 handleAuthResult); 50 }; 51 } 52 } 53 54 /** 55 * Start the file upload. 56 * 57 * @param {Object} evt Arguments from the file selector. 58 */ 59 function writeFile(evt) { 60 gapi.client.load('drive', 'v2', function () { 61 //var file = evt.target.files[0]; 62 var fileName = document.getElementById("fileName").value; 63 var content = document.getElementById("content").value; 64 console.log("fileName = "+fileName); 65 console.log("content = "+content); 66 insertFile(fileName,content); 67 }); 68 } 69 70 /** 71 * Insert new file. 72 * 73 * @param {fileName} 保存するファイル名 74 * @param {content} 保存するファイルの内容 75 * @param {Function} callback Function to call when the request is complete. 76 */ 77 function insertFile(fileName,content, callback) { 78 const boundary = '-------314159265358979323846'; 79 const delimiter = "\r\n--" + boundary + "\r\n"; 80 const close_delim = "\r\n--" + boundary + "--"; 81 82 var contentType = 'text/plain'; 83 var metadata = { 84 'title': fileName, 85 'mimeType': contentType 86 }; 87 88 var base64Data = utf8_to_b64(content); 89 var multipartRequestBody = delimiter + 90 'Content-Type: application/json\r\n\r\n' + JSON.stringify(metadata) + delimiter + 91 'Content-Type: ' + contentType + '\r\n' + 92 'Content-Transfer-Encoding: base64\r\n' + 93 '\r\n' + base64Data + close_delim; 94 95 var request = gapi.client.request({ 96 'path': '/upload/drive/v2/files', 97 'method': 'POST', 98 'params': { 99 'uploadType': 'multipart' 100 }, 101 'headers': { 102 'Content-Type': 'multipart/mixed; boundary="' + boundary + '"' 103 }, 104 'body': multipartRequestBody 105 }); 106 if(!callback) { 107 callback = function (file) { 108 alert("保存しました。"); 109 console.log(file) 110 }; 111 } 112 request.execute(callback); 113 114 } 115 116 // from http://ecmanaut.blogspot.jp/2006/07/encoding-decoding-utf8-in-javascript.html 117 function utf8_to_b64(str) { 118 return window.btoa( unescape(encodeURIComponent( str )) ); 119} 120 121</script> 122<script type="text/javascript" src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script> 123<!--Add a file picker for the user to start the upload process --> 124<div id="main" style="display: none"> 125<p> 126<label>ファイル名</label> 127<input type="text" id="fileName"> 128</p> 129<p> 130<label>内容</label> 131<textarea id="content" cols="50" rows="5"></textarea> 132</p> 133<p> 134 <button id="saveBtn"> 135 保存 136 </button> 137</p> 138<input type="button" id="authorizeButton" style="display: none" value="Authorize" /> 139</div>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問