Google Drive API中的接收错误403-超过未经验证使用的每日限制.继续使用需要注册



我正在为ruby使用Google Drive API,并试图将文件插入到用户驱动器中。我可以成功地插入文件,但当我尝试获得文件自链接时,这是返回的URLhttps://www.googleapis.com/drive/v2/files/1PxrbKaO2xwOonUO0TB0FO3pkZDnSmRKTvIUmXw0vL6w,这意味着我已经超过了未经验证的请求数量。

嗯,我很确定我已经通过了身份验证,因为我遵循了所有的教程,代码几乎相同。

有人知道为什么会发生这种事吗?为什么我可以插入文件,但不能查看它的自链接?

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

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, title, description, 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, title, description, mime_type, parent_id)
        drive = client.discovered_api('drive', 'v2')
        file = create_file_schema drive, title, description, mime_type, 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, title, description, mime_type, 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?文件自链接仍然需要授权才能访问,您应该向该URL发送授权的GET请求以检索其元数据。

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

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

此外,请查看文档,了解如何从Drive下载文件的指导,包括如何在浏览器中打开文件:

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

即使我也面临同样的错误,然后我用https://www.googleapis.com/auth/drive.file然后它开始正常工作。请尝试替换作用域或将此作用域添加到作用域列表中。希望这对你有用。

-Dhanush

相关内容

  • 没有找到相关文章

最新更新