【问题标题】:Using React-native GDrive can't upload file to Google Drive使用 React-native GDrive 无法将文件上传到 Google Drive
【发布时间】:2020-04-22 12:58:25
【问题描述】:

我在我的项目中使用了 react-native-google-drive-api-wrapper,我可以成功登录,但仍然无法在 google drive 上创建或上传任何内容。我错过了什么?

    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":{}}}}

【问题讨论】:

  • 你能分享你收到的错误吗?
  • 请分享您使用的 API 包装器的链接。 gspread 对吗?
  • 你收到的错误是什么?
  • 这是 403 错误,当我 JSON.stringify(response) 时,这就是我得到的。 {"type":"default","status":403,"ok":false,"headers":{"map":{"content-type":"application/json; charset=UTF-8","不同":"Origin, X-Origin","date":"2020 年 4 月 22 日星期三 14:33:41 GMT","www-authenticate":"Bearer realm=\"accounts.google.com/\", 错误=insufficient_scope, scope=\"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 }跨度>

标签: react-native google-drive-api access-token


【解决方案1】:

根据您的错误:

error=insufficient_scope, 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 scopes

【讨论】:

  • 我应该如何验证?
  • GoogleSignin.getTokens() 在这个函数上
  • GoogleSignin.configure() 接受选项,其中之一是范围
  • 我已经定义了链接中定义的范围。,我应该在这里特别定义什么?
  • 好的,我会试一试,如果问题解决了会告诉你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-10
相关资源
最近更新 更多