我在项目中使用了react-native-google-drive-api-wrapper,虽然可以成功登录,但仍然无法在Google驱动器上创建或上传任何内容。我想念什么?
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);}
我想念的是什么?这是访问accessToken的正确方法,还是有其他错误,请帮忙。
在我得到console.log(JSON.stringify(response))之后
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":{}}}}
由于您的错误:
error = insufficient_scope,scope = \“ googleapis.com/auth/drive \”
似乎您没有授予应用访问范围"https://www.googleapis.com/auth/drive"
的权限。
要在云端硬盘中创建文件,您需要应用用户的同意才能使用该范围。
[在请求身份验证令牌时验证您正在请求访问该范围。
您正在使用GoogleSignin
,因此在请求令牌之前将范围参数传递到GoogleSignin.configure()
。