nativescript-google-drive
一个 Nativescript 插件,用于在 Google Drive 上上传、下载和删除文件。
npm i --save nativescript-google-drive
- 版本:1.0.3
- GitHub: https://github.com/juanchinovas/nativescript-google-drive
- NPM: https://npmjs.net.cn/package/nativescript-google-drive
- 下载
- 昨天:0
- 上周:1
- 上个月:4
Nativescript Google Drive

Google Drive
上传、检索和从您的 Nativescript 应用程序删除 Google Drive 上的文件
先决条件
首先,如果您没有 Google 账户,请 创建一个 (我认为大多数人都有 😝)。
前往 console.developers.google.com
- 创建一个项目
- 使用菜单选择 APIs and Services 选项,然后选择 Dashboard
- 使用顶部栏上的 Enable APIs and Services 按钮添加 Google Drive API
- 在 Credentials 中,使用顶部栏上的 Create Credentials 按钮创建凭据
- 为 Android 创建 API 密钥
- 为 Android 和 iOS 创建 OAuth 2.0 客户端 ID 凭据
- 在 OAuth Consent 屏幕选项中创建或编辑,并按照说明操作
- 添加作用域 drive.file、drive.appdata、drive.metadata 或 drive


iOS
您需要前往 Google 开发者控制台,打开为 iOS 创建的 OAuth 2.0 客户端 ID,并复制 iOS URL scheme
(REVERSED_CLIENT_ID)或下载 plist。
将以下代码复制并粘贴到 App_Resources/iOS/Info.plist
中,并替换 REVERSED_CLIENT_ID
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>[Add here the REVERSED_CLIENT_ID]</string>
</array>
</dict>
</array>
安装
要将此 Nativescript 插件安装到您的项目中,只需在控制台输入以下命令或(复制并粘贴)
tns plugin add nativescript-google-drive
用法
import { isIOS } from "tns-core-modules/platform";
import { GoogleDriveHelper, SPACES, Config } from "nativescript-google-drive";
import * as ThreadWorker from "nativescript-worker-loader!nativescript-google-drive/thread-worker";
const config: Config = {
space: SPACES.APP_DATA_FOLDER, /*[DRIVE|APP_DATA_FOLDER]*/
worker: ThreadWorker
};
// iOS need this extra the clientID
if (isIOS) {
config.clientId = [CLIENT_ID];/*OAuth 2.0 client Id*/
}
GoogleDriveHelper.signInOnGoogleDrive(config)
.then((helper: GoogleDriveHelper) => {
// TODO
})
.catch((err) => {
// handler error
});
如果您在使用此插件时遇到与 Angular Nativescript 应用程序和 worker loader 相关的问题,请 阅读此内容 并查看 Angular 示例应用程序。
API
signInOnGoogleDrive
/**
* This method start Google SignIn flow and ask for Gogole Drive permissions to the user
* and initialize a drive helper class
* @static @function
*
* @param {Config} config
*
* @returns {Promise<GoogleDriveHelper>}
*/
static signInOnGoogleDrive(config: Config): Promise<GoogleDriveHelper>;
const config: Config = {
space: SPACES.APP_DATA_FOLDER, /*[DRIVE|APP_DATA_FOLDER]*/
worker: ThreadWorker
};
// iOS needs the clientID
if (isIOS) {
config.clientId = [CLIENT_ID];/*OAuth 2.0 client Id*/
}
GoogleDriveHelper.signInOnGoogleDrive(config)
.then((helper: GoogleDriveHelper) => {
// TODO
})
.catch((err) => {
// handler error
});
Config
接口的属性
属性 | 类型 | 描述 |
---|---|---|
space | 字符串 |
在 Android, iOS 上必需。指定要工作的驱动器作用域或空间 SPACES.APP_DATA_FOLDER 或 SPACES.DRIVE |
worker | 对象 |
在 Android, iOS 上必需。执行所有操作的线程 |
clientId | 字符串 |
在 iOS 上必需。OAuth 2.0 客户端 ID |
extraDriveScopes | 字符串数组 |
在 Android, iOS 上可选。指定更多作用域 |
createFile
/**
* Create a file with the specified metadata in Google Drive
*
* @param {FileInfoContent} fileInfo file metadata
*
* @returns {Promise<string>} created file id
*/
createFile(fileInfo: FileInfoContent): Promise<string>;
FileInfoContent
接口的属性
属性 | 类型 | 描述 |
---|---|---|
content | 字符串或文件 |
文件内容 |
FileInfoContent
继承自FileInfo
updateFile
/**
* Update a file content in Google Drive.
* If you want to update the metadata, you have to required permission to metadata scope to the user.
*
* @param {FileInfoContent} fileInfo file metadata
*
* @returns {Promise<string>} created file id
*/
updateFile(fileInfo: FileInfoContent): Promise<boolean>;
readFileContent
/**
* Read the content of plain text file
* @param {string} driveFileId
*
* @returns {Promise<string>} text contained in the file
*/
readFileContent(driveFileId: string): Promise<string>;
deleteFile
/**
* Delete a file
* @param {string} driveFileId
*
* @returns {Promise<boolean>} deleted or not
*/
deleteFile(driveFileId: string): Promise<boolean>;
downloadFile
/**
* Download a file
* @param {string} driveFileId
*
* @returns {Promise<File>} file downloaded
*/
downloadFile(driveFileId: string): Promise<File>;
uploadFile
/**
* Upload a file with the specified metadata in Google Drive
*
* @param {FileInfo} fileInfo file metadata
*
* @returns {Promise<string>} uploaded file id
*/
uploadFile(fileInfo: FileInfo): Promise<string>;
FileInfo
接口的属性
属性 | 类型 | 描述 |
---|---|---|
name | 字符串 |
必需。文件名 |
mimeType | 字符串 |
文件 MIME 类型 |
id | 字符串 |
文件 ID |
description | 字符串 |
文件描述 |
parentId | 字符串 |
文件父 ID |
createdTime | 日期 |
文件上传的时间 |
size | 数字 |
文件大小(kb) |
listFilesByParent
/**
* List all the files contained in the parent or root folder
* @param {string} parentId parent folder OPTIONAL
*
* @returns {Promise<Array<FileInfo>>} file list
*/
listFilesByParent(parentId?: string): Promise<Array<FileInfo>>;
searchFiles
/**
* Search files in Google Drive with the given metadata.
*
* @param {FileInfo} fileInfo file metadata to search for
*
* @returns {Promise<Array<FileInfo>>} file list matched
*/
searchFiles(fileInfo: FileInfo): Promise<Array<FileInfo>>;
createFolder
/**
* Create a folder with the given metadata. The content property is ignore
* @param {FileInfo} fileInfo folder metadata
*
* @returns {Promise<string>} created folder id
*/
createFolder(fileInfo: FileInfo): Promise<string>;
findFolder
/**
* Find folders by name
*
* @param {string} name
*
* @returns {Promise<Array<FileInfo>>} folder list
*/
findFolder(name: string): Promise<Array<FileInfo>>;
signOut
/**
* Disconnect the google drive account
* @returns {Promise<boolean>}
*/
signOut(): Promise<boolean>;