加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – Google Drive API中的接收错误403 – 超出未经身份验证

发布时间:2020-12-17 02:20:29 所属栏目:百科 来源:网络整理
导读:我正在使用Google Drive API作为 ruby,我正在尝试将文件插入用户驱动器.我可以成功插入文件,但是当我尝试获取文件Self Link时,这是返回 https://www.googleapis.com/drive/v2/files/1PxrbKaO2xwOonUO0TB0FO3pkZDnSmRKTvIUmXw0vL6w的URL,表示我已超过未经身份
我正在使用Google Drive API作为 ruby,我正在尝试将文件插入用户驱动器.我可以成功插入文件,但是当我尝试获取文件Self Link时,这是返回 https://www.googleapis.com/drive/v2/files/1PxrbKaO2xwOonUO0TB0FO3pkZDnSmRKTvIUmXw0vL6w的URL,表示我已超过未经身份验证的请求数.

好吧,我很确定我已通过身份验证,因为我遵循了所有教程并且代码几乎相同.

谁知道为什么会这样?为什么我可以插入文件,但无法查看它的Self Link?

以下是用户允许应用程序进行离线访问后,在驱动器中创建文档的代码:

SCOPES = [
              'https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/userinfo.email','https://www.googleapis.com/auth/userinfo.profile'
          ]

class DriveAuthenticator

    def self.create_document(user,title,description,parent_title,user_array)
        client = DriveAuthenticator.initiate_client
        if user.google_id.present?
            if DriveAuthenticator.authenticate_client client,user
                parent_id = get_parent_id client,parent_title
                file_data = insert_empty_file client,DOCUMENT_MIME_TYPE,parent_id
                return file_data.selfLink
            end
        end
    end

    private
    def self.initiate_client
        client = Google::APIClient.new(:application_name => 'stuff_app')

        credentials = Google::APIClient::ClientSecrets.load

        client.authorization.client_id = credentials.client_id
        client.authorization.client_secret = credentials.client_secret
        client.authorization.redirect_uri = credentials.redirect_uris.first
        client.authorization.scope = SCOPES
        client
    end

    private
    def self.get_token_by_refresh_token(client,refresh_token)
        client.authorization.refresh_token = refresh_token
        client.authorization.grant_type = 'refresh_token'
        client.authorization.fetch_access_token!
    end

    private
    def self.insert_empty_file(client,mime_type,parent_id)
        drive = client.discovered_api('drive','v2')
        file = create_file_schema drive,parent_id
        result = client.execute(
          :api_method => drive.files.insert,:body_object => file)
        if result.status == 200
            result.data
        else
            puts "An error occurred: #{result.data['error']['message']}"
            nil
        end
    end

    private
    def self.create_file_schema(drive,parent_id)
        file = drive.files.insert.request_schema.new({
            'title' => title,'description' => description,'mimeType' => mime_type
        })
        file.parents = [{'id' => parent_id}]
        file
    end

end

解决方法

您是否尝试在浏览器中打开该网址?文件自链接仍然需要访问授权,您应该向该URL发送授权的GET请求以检索其元数据.

Google Drive SDK文档中的参考指南显示了如何获取文件元数据和内容:

https://developers.google.com/drive/v2/reference/files/get

另请查看文档以获取有关如何从云端硬盘下载文件的指导,包括如何在浏览器中打开文件:

https://developers.google.com/drive/manage-downloads

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读