【问题标题】:How to get last modifying user email address from Google Drive Ruby API如何从 Google Drive Ruby API 获取最后修改用户的电子邮件地址
【发布时间】:2017-08-29 19:58:38
【问题描述】:

我是 Google Drive API 的新手,我很难在文档和其他帖子中找到答案。我正在编写一个 Ruby 脚本来将文件移入/移出 Google 团队驱动器,并希望能够查看每个文件的最后修改用户的电子邮件地址。我可以在 API Explorer 中运行我的查询并获得我正在寻找的结果,但是当我从我的脚本运行相同的查询时,我只能看到最后修改用户的显示名称和其他几个数据点,但没有电子邮件地址。我正在使用我创建的服务帐户通过 API 进行身份验证,并已授予该服务帐户对团队驱动器和域范围委派权限的完全访问权限。下面是我的代码。谁能给点建议?

require 'google/apis/drive_v3'
require 'googleauth'

SCOPES = [
  Google::Apis::DriveV3::AUTH_DRIVE,
  Google::Apis::DriveV3::AUTH_DRIVE_APPDATA,
  Google::Apis::DriveV3::AUTH_DRIVE_FILE,
  Google::Apis::DriveV3::AUTH_DRIVE_METADATA,
  Google::Apis::DriveV3::AUTH_DRIVE_METADATA_READONLY,
  Google::Apis::DriveV3::AUTH_DRIVE_PHOTOS_READONLY,
  Google::Apis::DriveV3::AUTH_DRIVE_READONLY
]

ENV['GOOGLE_APPLICATION_CREDENTIALS'] = "/path/to/my/service/account/creds.json"

service = Google::Apis::DriveV3::DriveService.new
service.authorization = Google::Auth.get_application_default(SCOPES)

response = service.list_files(
  q: "'team drive id' in parents",
  include_team_drive_items: true,
  supports_team_drives: true
)

编辑:

响应对象如下:

#<Google::Apis::DriveV3::FileList:<memory location> @files=[#<Google::Apis::DriveV3::File:<memory location> @capabilities=#<Google::Apis::DriveV3::File::Capabilities:<memory location> @can_add_children=false, @can_change_viewers_can_copy_content=true, @can_comment=true, @can_copy=true, @can_delete=true, @can_download=true, @can_edit=true, @can_list_children=false, @can_move_item_into_team_drive=true, @can_move_team_drive_item=true, @can_read_revisions=true, @can_read_team_drive=true, @can_remove_children=false, @can_rename=true, @can_share=true, @can_trash=true, @can_untrash=true>, @created_time=<created time>, @explicitly_trashed=false, @has_augmented_permissions=true, @has_thumbnail=true, @icon_link=<icon link>, @id=<id>, @is_app_authorized=false, @kind=\"drive#file\", @last_modifying_user=#<Google::Apis::DriveV3::User:<memory location> @display_name=<display name>, @kind=\"drive#user\", @me=false, @permission_id=<permission id>, @photo_link=<photo link>>, @mime_type=<mime type>, @modified_by_me=false, @modified_time=<modified time>, @name=<name>, @parents=[<parent id>], @quota_bytes_used=0, @spaces=[\"drive\"], @starred=false, @team_drive_id=<team drive id>, @thumbnail_link=<thumbnail link>, @thumbnail_version=<thumbnail version>, @trashed=false, @version=<version>, @viewed_by_me=false, @viewers_can_copy_content=true, @web_view_link=<web view link>], @incomplete_search=false, @kind=\"drive#fileList\">

【问题讨论】:

    标签: google-drive-api


    【解决方案1】:

    我将用 Javascript 演示这个概念,你将它翻译成 Ruby。有两种方法可以做到这一点。

    1.对所有文件使用 Files.list

    既然我看到您使用的是Files.list,让我们先回答这个问题。您必须知道在 Files resource 中找到的 JSON 元数据的层次结构。

    要访问lastModifyingUser 属性,请使用 file.lastModifyingUser.emailAddress 其中filevar file = response.result.files;,其中包含驱动器文件的数组列表。

    2。对特定文件使用 Revisions.get

    要获取lastModifyingUser,您可以使用Revisions.get。使用此方法时,您必须知道revisionId(这是您使用revisions.list 获得的修订号)。您可以尝试使用 1 作为revisionId 进行测试。

    这是我的 JS 代码:

     function revisionGet() {
            gapi.client.drive.revisions.get({
              'fileId': '123456789ABCDEFG',
              'revisionId': 1,
              'fields': '*'
            }).then(function(response) {
      
              var getResponse = response;
              console.log(getResponse.result);
    
              appendPre('displayName: ' + getResponse.result.lastModifyingUser.displayName );
              appendPre('email: ' + getResponse.result.lastModifyingUser.emailAddress );
            });
          }
    

    在“字段”属性中传递的wildcard * 只是意味着,返回所有元数据。 appendPre 只是在 HTML 中显示的自定义函数。 重要的是要知道如何使用

    访问emailAddress 属性
    response.result.lastModifyingUser.emailAddress
    

    我的 HTML 输出:

    displayName: noogui
    email: mypersonalemail@gmail.com
    

    要查看修订元数据的 JSON 层次结构,请访问 revision file resource

    【讨论】:

    • 感谢您的回答,但我已经知道 JSON 元数据的层次结构。问题是file.last_modifying_user.email_address =&gt; nil
    • 你能打印你的“响应”对象吗?
    • 我在帖子中添加了回复,因为评论太长了。
    • 我能够使用我在其他帖子中找到的方法获取电子邮件地址。我只是把它翻译成Ruby。 link
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    相关资源
    最近更新 更多