如何从Google Drive Ruby API获取上次修改用户的电子邮件地址 [英] How to get last modifying user email address from Google Drive Ruby API

查看:81
本文介绍了如何从Google Drive Ruby API获取上次修改用户的电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Google Drive API的新手,我很难在文档和其他文章中找到答案.我正在编写一个Ruby脚本,以将文件移至Google Team Drive或从Google Team Drive移出文件,并且希望能够看到这些文件中每个文件的最后修改用户的电子邮件地址.我可以在API资源管理器中运行查询并获得所需的结果,但是当我从脚本运行相同的查询时,我只能看到最后修改用户的显示名称和其他几个数据点,但没有电子邮件地址.我使用的是我创建的用于通过API进行身份验证的服务帐户,并已授予该服务帐户对Team Drive的完全访问权限和域范围的委托权限.下面是我的代码.谁能提供任何建议?

I'm new to the Google Drive API and I'm having a hard time finding my answer in the documentation and other posts. I'm writing a Ruby script to move files to/from a Google Team Drive and would like to be able to see each of those files' last modifying user's email address. I can run my query in the API Explorer and get the result I'm looking for, but when I run the same query from my script, I can only see the last modifying user's display name and a couple other data points, but no email address. I'm using a service account I created to authenticate with the API and have given that service account Full access on the Team Drive and Domain-wide Delegation permissions. Below is my code. Can anyone offer any suggestions?

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\">

推荐答案

我将演示Javascript中的概念,然后将其转换为Ruby.有两种方法可以做到这一点.

I'll demo the concept in Javascript, you translate it to Ruby. Two ways you can do this.

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

由于我看到您正在使用 Files.list ,首先回答.您必须了解文件资源.

Since I see you're using Files.list let's answer that first. You have to know the hierarchy of your JSON metadata which is found in Files resource.

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

To access lastModifyingUser property use file.lastModifyingUser.emailAddress where file is var file = response.result.files; which contains an array list of your drive files.

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

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

To get lastModifyingUser, you can use Revisions.get. When using this method you have to know the revisionId (it's a revision number you obtain using revisions.list). You can try using 1 as revisionId for testing purposes.

这是我的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 );
        });
      }

在'fields'属性中传递的wildcard *只是意味着返回所有元数据. appendPre只是一个以HTML显示的自定义函数. 重要的一点是要知道如何使用属性访问

The wildcard * being passed in 'fields' property just means, return all metadata. appendPre is just a custom function to display in HTML. The important bit is to know how to access the emailAddress property using

response.result.lastModifyingUser.emailAddress

我的HTML输出:

displayName: noogui
email: mypersonalemail@gmail.com

要查看修订元数据的JSON层次结构,请访问修订文件资源.

To see the JSON hierarchy of revision metadata go to revision file resource.

这篇关于如何从Google Drive Ruby API获取上次修改用户的电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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