在 C# 中使用 Google 数据传输 API 驱动器传输
Drive transfers using Google Data Transfer API in C#
我正在编写此代码以使用启用了域范围委派的服务帐户使用驱动器传输 API 将所有驱动器数据从一个用户传输到另一个用户。
string oldowner = "oldowner@xyz.com";
string newowner = "newowner@xyz.com";
DirectoryService dir_service = Build_Directory_Service();
var olduser = dir_service.Users.Get(oldowner).Execute();
var newuser = dir_service.Users.Get(newowner).Execute();
DataTransferService dtservice = Build_DataTransfer_Service();
DataTransfer dataXfer = new DataTransfer();
dataXfer.OldOwnerUserId = olduser.Id;
dataXfer.NewOwnerUserId = newuser.Id;
dataXfer.ApplicationDataTransfers[0].ApplicationId = 55656082996;
dataXfer.ApplicationDataTransfers[0].ApplicationTransferParams[0].Key= "PRIVACY_LEVEL";
dataXfer.ApplicationDataTransfers[0].ApplicationTransferParams[0].Value.Add("PRIVATE");
dataXfer.ApplicationDataTransfers[0].ApplicationTransferParams[0].Value.Add("SHARED");
Console.WriteLine("Starting Data transfer from " + oldowner + " to " + newowner);
dtservice.Transfers.Insert(dataXfer).Execute();
但是我得到这个错误:
"Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object."
堆栈跟踪指向抛出异常的这一行:
dataXfer.ApplicationDataTransfers[0].ApplicationId = 55656082996;
我正在关注 API 的文档:https://developers.google.com/admin-sdk/data-transfer/v1/reference/transfers#resource
但我可能没有以正确的方式设置传输资源的值。我需要帮助将值正确分配给传输资源。
谢谢,
好的,根据您的评论,我会添加
dataXfer.ApplicationDataTransfers = new List<ApplicationDataTransfer>();
在抛出的那行之前。毕竟有一个null对象,又不像ApplicationDataTransfers
初始化的样子。
为了大家的利益,我贴出我的固定代码。基本上这里的需要是初始化代码后面需要的每个列表。希望这对其他人有帮助。
DataTransferService dtservice = Build_DataTransfer_Service();
DataTransfer dataXfer2 = new DataTransfer()
{
Kind = "admin#datatransfer#DataTransfer",
OldOwnerUserId = olduser.Id,
NewOwnerUserId = newuser.Id,
ApplicationDataTransfers = new List<ApplicationDataTransfer>()
};
ApplicationDataTransfer item = new ApplicationDataTransfer()
{
ApplicationId = (long)55656082996,
ApplicationTransferParams = new List<ApplicationTransferParam>()
};
item.ApplicationTransferParams.Add(new ApplicationTransferParam()
{
Key = "PRIVACY_LEVEL",
Value =
new List<string>() { "PRIVATE",
"SHARED" }
});
dataXfer2.ApplicationDataTransfers.Add(item);
Console.WriteLine("Starting Data transfer from " + oldowner + " to " + newowner);
TransfersResource.InsertRequest req = dtservice.Transfers.Insert(dataXfer2);
var resp2=req.Execute();
Console.WriteLine("Initial OverTransferStatusCode = " + resp2.OverallTransferStatusCode);
Console.WriteLine("TransferID = " + dataXfer2.Id);
我正在编写此代码以使用启用了域范围委派的服务帐户使用驱动器传输 API 将所有驱动器数据从一个用户传输到另一个用户。
string oldowner = "oldowner@xyz.com";
string newowner = "newowner@xyz.com";
DirectoryService dir_service = Build_Directory_Service();
var olduser = dir_service.Users.Get(oldowner).Execute();
var newuser = dir_service.Users.Get(newowner).Execute();
DataTransferService dtservice = Build_DataTransfer_Service();
DataTransfer dataXfer = new DataTransfer();
dataXfer.OldOwnerUserId = olduser.Id;
dataXfer.NewOwnerUserId = newuser.Id;
dataXfer.ApplicationDataTransfers[0].ApplicationId = 55656082996;
dataXfer.ApplicationDataTransfers[0].ApplicationTransferParams[0].Key= "PRIVACY_LEVEL";
dataXfer.ApplicationDataTransfers[0].ApplicationTransferParams[0].Value.Add("PRIVATE");
dataXfer.ApplicationDataTransfers[0].ApplicationTransferParams[0].Value.Add("SHARED");
Console.WriteLine("Starting Data transfer from " + oldowner + " to " + newowner);
dtservice.Transfers.Insert(dataXfer).Execute();
但是我得到这个错误:
"Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object."
堆栈跟踪指向抛出异常的这一行:
dataXfer.ApplicationDataTransfers[0].ApplicationId = 55656082996;
我正在关注 API 的文档:https://developers.google.com/admin-sdk/data-transfer/v1/reference/transfers#resource
但我可能没有以正确的方式设置传输资源的值。我需要帮助将值正确分配给传输资源。
谢谢,
好的,根据您的评论,我会添加
dataXfer.ApplicationDataTransfers = new List<ApplicationDataTransfer>();
在抛出的那行之前。毕竟有一个null对象,又不像ApplicationDataTransfers
初始化的样子。
为了大家的利益,我贴出我的固定代码。基本上这里的需要是初始化代码后面需要的每个列表。希望这对其他人有帮助。
DataTransferService dtservice = Build_DataTransfer_Service();
DataTransfer dataXfer2 = new DataTransfer()
{
Kind = "admin#datatransfer#DataTransfer",
OldOwnerUserId = olduser.Id,
NewOwnerUserId = newuser.Id,
ApplicationDataTransfers = new List<ApplicationDataTransfer>()
};
ApplicationDataTransfer item = new ApplicationDataTransfer()
{
ApplicationId = (long)55656082996,
ApplicationTransferParams = new List<ApplicationTransferParam>()
};
item.ApplicationTransferParams.Add(new ApplicationTransferParam()
{
Key = "PRIVACY_LEVEL",
Value =
new List<string>() { "PRIVATE",
"SHARED" }
});
dataXfer2.ApplicationDataTransfers.Add(item);
Console.WriteLine("Starting Data transfer from " + oldowner + " to " + newowner);
TransfersResource.InsertRequest req = dtservice.Transfers.Insert(dataXfer2);
var resp2=req.Execute();
Console.WriteLine("Initial OverTransferStatusCode = " + resp2.OverallTransferStatusCode);
Console.WriteLine("TransferID = " + dataXfer2.Id);