react-native - Using React-native GDrive can't upload file to Google Drive
问题描述
I have used react-native-google-drive-api-wrapper in my project, I can log in successfully but still, I can't create or upload anything on google drive. What am I missing?
signIn = async () => {
try {
await GoogleSignin.hasPlayServices();
const userInfo = await GoogleSignin.signIn();
const token= (await GoogleSignin.getTokens()).accessToken;
// console.log();
this.setState({ userInfo : userInfo, accToken: token, email : userInfo.user.email});
// alert('info is '+ userInfo.user.familyName);
GDrive.setAccessToken(this.state.accToken);
GDrive.init();
// alert("gdrive done")
if(GDrive.isInitialized()){
alert("Google Drive is Initialized Now!!")
const contents = "My text file contents";
GDrive.files.createFileMultipart(
contents,
"text/plain", {
parents: ["root"],
name: "text2.txt"
},
false);
// this.createAFile();
}
} catch (error) {
alert('error is '+ error);}
What am I missing? is this the right way to access the accessToken, or there is some other mistake, please help.
after console.log(JSON.stringify(response)) I Get
LOG {"type":"default","status":403,"ok":false,"headers":{"map":
{"content-
type"
:"application/json; charset=UTF-8","vary":"Origin, X-
Origin","date":"Wed, 22 Apr
2020 14:33:41 GMT","www-authenticate":"Bearer
realm=\"https://accounts.google.c
om/\", error=insufficient_scope,
scope=\"https://www.googleapis.com/auth/drive\"
","server":"UploadServer","alt-svc":"quic=\":443\"; ma=2592000;
v=\"46,43\",h3-Q
050=\":443\"; ma=2592000,h3-Q049=\":443\"; ma=2592000,h3-
Q048=\":443\"; ma=25920
00,h3-Q046=\":443\"; ma=2592000,h3-Q043=\":443\"; ma=2592000,h3-
T050=\":443\"; m
a=2592000","content-length":"306","x-guploader-
uploadid":"AAANsUm9Zk5l-FX8wKLBku
ZUaYyxfMr403jozVOIj8Q7zt5AGORvLIEw0hUjNSfUsaiMt06LBTkJDa9SwzB98QCjkR8"}},"url" :"
https://www.googleapis.com/upload/drive/v3/files?
uploadType=multipart","_bodyIni
t":{"_data":{"size":306,"offset":0,"blobId":"29e54a64-a7cd-4b5b-8c2f-
6fe63d9391f
b","__collector":{}}},"_bodyBlob":{"_data"
{"size":306,"offset":0,"blobId":"29e5
4a64-a7cd-4b5b-8c2f-6fe63d9391fb","__collector":{}}}}
解决方案
从你的错误:
错误=insufficient_scope,范围=\"googleapis.com/auth/drive\"
看来您没有授予应用程序访问范围的权限"https://www.googleapis.com/auth/drive"
。
要在云端硬盘中创建文件,您需要应用用户对该范围的同意。
验证您在请求身份验证令牌时是否请求访问该范围。
例子:
您正在使用,因此在请求访问令牌之前GoogleSignin
将范围选项传递给。GoogleSignin.configure()
GoogleSignin.configure({
scopes: ['https://www.googleapis.com/auth/drive'],
...
});
文档: Drive API v3 范围
推荐阅读
- typescript - “意外的标记 。” 当使用可选的 chaning "?." 运行 mocha 时的语法
- python-3.x - 我想在 python 中计算成绩,但如果任何个别科目的分数低于 35,我不想显示成绩
- javascript - Brave 浏览器无法与 Puppeteer 一起使用。Brave 无法读取和写入其 Data 目录
- css - grid-row: 1 / -1 实际上是如何工作的?(需要参考)
- mysql - 使用 nodejs 到 MariaDB 的 Angular POST
- ruby-on-rails - 用于 nil:NilClass 的 Rails 控制器 RSpec 未定义方法“编码”
- node.js - MongoDB 合并来自不同集合的两个聚合
- python - Python:如何用元组中的值替换多次出现的字符串
- css - 如何使用 SPFX 将 CSS 或 JavaScript 注入 SharePoint 现代页面
- parse-platform - 解析服务器令牌验证