在Google云端硬盘API中收到错误403 - 超出未认证使用的每日限额。继续使用需要注册 [英] Receiving error 403 in Google Drive API - Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup

查看:240
本文介绍了在Google云端硬盘API中收到错误403 - 超出未认证使用的每日限额。继续使用需要注册的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google Drive API进行ruby操作,并试图将文件插入用户驱动器。我可以插入成功的文件,但是当我尝试获取文件Self Link时,这是返回的URL https://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'
]

类DriveAuthenticator

def self.create_document(user,title,description,parent_title,user_array)
client = DriveAuthenticator.initiate_client
if user.google_id 。当下?
如果DriveAuthenticator.authenticate_client客户端,用户
parent_id = get_parent_id客户端,parent_title
file_data = insert_empty_file客户端,标题,说明,DOCUMENT_MIME_TYPE,parent_id
返回file_data.selfLink
结束
结束
结束

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

凭证= 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(客户端,refresh_token)
client.authorization.re fresh_token = refresh_token
client.authorization.grant_type ='refresh_token'
client.authorization.fetch_access_token!
end
$ b private
def self.insert_empty_file(客户端,标题,说明,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发生错误:#{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}]
文件
结束

结束


解决方案

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



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

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



另请参阅文档以获取关于如何从云端硬盘下载文件,包括如何在浏览器中打开它:



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


I'm using the Google Drive API for ruby, and I'm trying to insert a file into an user drive. I can insert the file with success, but then, when I try to get the file Self Link, this is the URL returned https://www.googleapis.com/drive/v2/files/1PxrbKaO2xwOonUO0TB0FO3pkZDnSmRKTvIUmXw0vL6w, that says that i have exceeded the number of unauthenticated requests.

Well, I'm pretty sure I'm authenticated, because I followed all the tutorials and the code is almost the same.

Anyone knows why is this happening? why can I insert the file, but can't view it's Self Link?

Here is the code of the creation of a document in the drive, after the user allows the app with offline access:

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

解决方案

Are you trying to open that url in your browser? The file self link still needs authorization to be accessed and you should send an authorized GET request to that URL to retrieve its metadata.

The Reference guide in the Google Drive SDK documentation shows how to get file metadata and content:

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

Also check the documentation for guidance on how to download a file from Drive, including how to open it in a browser:

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

这篇关于在Google云端硬盘API中收到错误403 - 超出未认证使用的每日限额。继续使用需要注册的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆