- この記事は、Google Drive™ APIに関する記事を和訳したものです。
- 原文: Display the Google picker
- 元記事のライセンスは CC-BYで、この和訳記事のライセンスは CC-BYです。
- 自己責任でご利用ください。
- 和訳した時期は 2019年6月ころです。
ユーザに、Google Pickerを用いて、あなたのアプリにて Google Driveのファイルを開く、あるいは、アップロードさせることができます。 ユーザがファイルを選択した後、Google Picker APIは、あなたのアプリにファイル IDを送信するので、それは、ファイルにアクセスすることができます。
以下は、Google Pickerが、サインインしているユーザの、ウェブアプリ内の Driveファイルのリストを表示する方法です。
例
次のピッカーの例は、ウェブアプリの Open or Upload Drive filesボタンから開かれた、画像の選択/アップロードページを示しています。
この例は、AppId
値をセットする方法を示し、複数選択を有効にする、ナビゲーションペインを非表示にする、アプリの現在の OAuth 2.0トークンを用いてユーザアカウントを選択するといった、幾つかの役立つピッカー機能を組み込んでいます:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Picker Example</title>
<script type="text/javascript">
// The Browser API key obtained from the Google API Console.
// Replace with your own Browser API key, or your own key.
var developerKey = 'xxxxxxxYYYYYYYY-12345678';
// The Client ID obtained from the Google API Console. Replace with your own Client ID.
var clientId = "1234567890-abcdefghijklmnopqrstuvwxyz.apps.googleusercontent.com"
// Replace with your own project number from console.developers.google.com.
// See "Project number" under "IAM & Admin" > "Settings"
var appId = "1234567890";
// Scope to use to access user's Drive items.
var scope = ['https://www.googleapis.com/auth/drive'];
var pickerApiLoaded = false;
var oauthToken;
// Use the Google API Loader script to load the google.picker script.
function loadPicker() {
gapi.load('auth', {'callback': onAuthApiLoad});
gapi.load('picker', {'callback': onPickerApiLoad});
}
function onAuthApiLoad() {
window.gapi.auth.authorize(
{
'client_id': clientId,
'scope': scope,
'immediate': false
},
handleAuthResult);
}
function onPickerApiLoad() {
pickerApiLoaded = true;
createPicker();
}
function handleAuthResult(authResult) {
if (authResult && !authResult.error) {
oauthToken = authResult.access_token;
createPicker();
}
}
// Create and render a Picker object for searching images.
function createPicker() {
if (pickerApiLoaded && oauthToken) {
var view = new google.picker.View(google.picker.ViewId.DOCS);
view.setMimeTypes("image/png,image/jpeg,image/jpg");
var picker = new google.picker.PickerBuilder()
.enableFeature(google.picker.Feature.NAV_HIDDEN)
.enableFeature(google.picker.Feature.MULTISELECT_ENABLED)
.setAppId(appId)
.setOAuthToken(oauthToken)
.addView(view)
.addView(new google.picker.DocsUploadView())
.setDeveloperKey(developerKey)
.setCallback(pickerCallback)
.build();
picker.setVisible(true);
}
}
// A simple callback implementation.
function pickerCallback(data) {
if (data.action == google.picker.Action.PICKED) {
var fileId = data.docs[0].id;
alert('The user selected: ' + fileId);
}
}
</script>
</head>
<body>
<div id="result"></div>
<!-- The Google API Loader script. -->
<script type="text/javascript" src="https://apis.google.com/js/api.js?onload=loadPicker"></script>
</body>
</html>
AppId
およびクライアント IDは、ユーザのファイルへのアクセスを承認するために使用されており、同じアプリに含まれていなければなりません。
API Console内にて、AppIdは、開発者コンソールの "IAM & Admin" > "Settings" ページ上の "Project number" として識別されることができます。
ファイルを開くときピッカーからファイル IDを取得した後、アプリケーションは、files.getのリファレンスドキュメントに記載されているように、ファイルのメタデータをフェッチし、ファイルコンテンツをダウンロードすることができます。