Google Drive API上载命令ResponseBy始终为空C#
我正在研究一个由树莓Pi驱动的系统。作为这项工作的一部分,我正试图从Pi上传一个文本文件到我的谷歌硬盘。作为第一个测试,我有一个UWP应用程序来创建一个温度和湿度数据的文本文件,并尝试上传它 当它到达upload命令时,什么也没有发生。不会引发异常,但也不会上载任何文件。我在网上搜索了很多死胡同,但没有一个错误,我真的不知道从哪里开始找出文件没有上传的原因;我遵循谷歌的指示,就像我说的,没有什么会引发异常,它就是不起作用。。。任何建议都是非常受欢迎的,因为我花了很长时间在谷歌上搜索答案,现在什么也没找到 以下是uwp应用程序xaml页面背后的代码要点(如果您要求,我可以向您发送整个项目)。我没有在下面的代码中包含几个方法,因为只有方法Google Drive API上载命令ResponseBy始终为空C#,c#,raspberry-pi,google-drive-api,C#,Raspberry Pi,Google Drive Api,我正在研究一个由树莓Pi驱动的系统。作为这项工作的一部分,我正试图从Pi上传一个文本文件到我的谷歌硬盘。作为第一个测试,我有一个UWP应用程序来创建一个温度和湿度数据的文本文件,并尝试上传它 当它到达upload命令时,什么也没有发生。不会引发异常,但也不会上载任何文件。我在网上搜索了很多死胡同,但没有一个错误,我真的不知道从哪里开始找出文件没有上传的原因;我遵循谷歌的指示,就像我说的,没有什么会引发异常,它就是不起作用。。。任何建议都是非常受欢迎的,因为我花了很长时间在谷歌上搜索答案,现在什么
StoreValues()
让我立即了解它。这是没有抛出错误的项目的第一次迭代
public sealed partial class MainPage : Page
{
static string[] Scopes = { DriveService.Scope.Drive };
static string AppName = "Drive API .NET Quickstart";
static string fileName = "Environmental_Data";
static string fileExtension = ".txt";
private const int DHTPIN = 4;
private IDht dht = null;
private GpioPin dhtPin = null;
private DispatcherTimer sensorTimer = new DispatcherTimer();
private int counterWrite = 0;
private int counterStore = 0;
//the number on the end of the filename that will be incremented each time a file is produced.
//TODO remove old files and reset counter periodically
private int fileId = 1;
//read values every this many seconds
private int readInterval = 1;
//write a new line to the file every this many readings
private int writeCollectionSize = 2;
//send file to cloud every this many lines
private int storeCollectionSize = 4;
private StorageFolder Folder = ApplicationData.Current.LocalFolder;
public List<Environment> Readings;
public StorageFile File1;
public MainPage()
{
InitializeComponent();
InitializeLocalFiles(fileId);
InitializePi();
}
private async void InitializeLocalFiles(int fileId)
{
try
{
//make a new target file to save our data to
var newFile = $"{fileName}_{fileId.ToString()}{fileExtension}";
File1 = await Folder.CreateFileAsync(newFile, CreationCollisionOption.ReplaceExisting);
File1 = await Folder.GetFileAsync(newFile);
//let a human know where the first file is
Debug.WriteLine(File1.Path);
}
catch (Exception ex)
{
Debug.WriteLine("error incountered in InitializeLocalFiles: {0}", ex.Message);
}
}
private void InitializePi()
{
try
{
//set up the pins etc
dhtPin = GpioController.GetDefault().OpenPin(DHTPIN, GpioSharingMode.Exclusive);
dht = new Dht11(dhtPin, GpioPinDriveMode.Input);
//This is what the environment class looks like: public class Environment
//{ public decimal Temperature; public decimal Humidity; public DateTime DateTime; }
Readings = new List<Environment>();
sensorTimer.Interval = TimeSpan.FromSeconds(readInterval);
//add the tick to the clock and set it going
sensorTimer.Tick += sensorTimer_Tick;
sensorTimer.Start();
//initialise the app
temperatureMeter.Value = "OFF";
humidityMeter.Value = "OFF";
}
catch (Exception ex)
{
Debug.WriteLine("error incountered in InitialisePi: {0}", ex.Message);
}
}
private void sensorTimer_Tick(object sender, object e)
{
try
{
//every tick of the clock, we will read the sensor to get new values
readSensor();
//if we got to the end of the write collection, then it is time to aggregate the figures and write a line to the local file
if (counterWrite == writeCollectionSize)
{
counterWrite = 0;
Readings.AggregateListToCSV(File1);
//clear the list of readings ready for the next tick
Readings.Clear();
}
//if we got to the end of the store collection, it's time to send the local file on a trip around the world
if (counterStore == storeCollectionSize)
{
counterStore = 0;
StoreValues(File1);
//rather than deleting the file, we will make a new one for the next go-round
fileId += 1;
InitializeLocalFiles(fileId);
}
}
catch (Exception ex)
{
Debug.WriteLine("error incountered in sensorTimer_Tick: {0}", ex.Message);
}
}
private async void StoreValues(StorageFile file)
{
try
{
UserCredential credential;
string cred = @"{""installed"":{""client_id"":""* **REDACTED FOR FORUM QUESTION ***"",""redirect_uris"":[""urn:ietf:wg:oauth:2.0:oob"",""http://localhost""]}}";
byte[] byteArray = Encoding.UTF8.GetBytes(cred);
//I'm using credentials stored in memory, but you could use a json file
using (var stream =
//new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
new MemoryStream(byteArray))
{
//TODO find out why I have to copy the token.json file every time to the app folder - for some reason it does not copy over even though set to Copy Always
string credPath = "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
}
//set up the stuff that the Google Drive seems to require. I don't pretend to understand this bit
DriveService service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = AppName,
});
service.HttpClient.Timeout = TimeSpan.FromMinutes(100);
var fileMetadata = new Google.Apis.Drive.v3.Data.File()
{
Name = "My Report",
MimeType = "application/vnd.google-apps.file"
};
FilesResource.CreateMediaUpload request;
//using (var stream = new FileStream(file.Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var stream = File.Open(file.Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
request = service.Files.Create(fileMetadata, stream, "text/plain");
request.Fields = "id";
await request.UploadAsync();
}
//request.ResponsBody is always NULL - why??
var responseFile = request.ResponseBody;
//this line causes an error "attempted to read or write protected memory"
if (responseFile == null)
{
Debug.WriteLine($"** ERROR ** File {fileName}_{(fileId - 1).ToString()}{fileExtension} could not be uploaded.");
}
else
{
Debug.WriteLine($"File ID: {responseFile.Id.ToString()}");
}
}
catch(Exception ex)
{
Debug.WriteLine("error incountered in StoreValues: {0}", ex.Message);
}
}
}
我相信我已经准备好了所有的凭证等——每次部署时,我都必须将从凭证生成的token.jsn文件复制到pi上,但尽管如此,它似乎都在正常连接
虽然我开始对wpf的起源有一点了解,但这完全超出了我的知识范围,经过3周的断断续续地尝试解决这个问题后,我真的筋疲力尽了——任何建议都会非常棒。在完全公开的情况下,我对您的代码大加关注,懒得重新编写代码。如果替换
wait request.UploadAsync()
与请求。上传?您是否在正在进行身份验证的Google项目中启用了Google Drive服务?我放下我的懒马,重新编写了你的代码,我的子集工作了。您的凭证文件(client\u secret.json
)包含对项目的引用。此项目
就是您要针对其进行身份验证的项目。如果您获取该值(我们称之为${PROJECT}
)并将其替换为以下URL中的值,您应该会看到列出的“GoogleDriveAPI:{PROJECT}`。不过,听起来您可以查询驱动器以列出文件,但不能上载文件。对吗?这是我的示例:@XamDev在上面的注释中看到我的要点,因为它包含工作代码。@XamDev您似乎已经确定需要OAuth2客户端ID凭据。您无法将文件上载到“与我共享”文件夹。这是一个文件夹,表示与您(由其他人)共享的一组文件。在完全公开的情况下,我对您的代码进行了大量的研究,懒得重新编写代码。如果替换wait request.UploadAsync()
与请求。上传?您是否在正在进行身份验证的Google项目中启用了Google Drive服务?我放下我的懒马,重新编写了你的代码,我的子集工作了。您的凭证文件(client\u secret.json
)包含对项目的引用。此项目
就是您要针对其进行身份验证的项目。如果您获取该值(我们称之为${PROJECT}
)并将其替换为以下URL中的值,您应该会看到列出的“GoogleDriveAPI:{PROJECT}`。不过,听起来您可以查询驱动器以列出文件,但不能上载文件。对吗?这是我的示例:@XamDev在上面的注释中看到我的要点,因为它包含工作代码。@XamDev您似乎已经确定需要OAuth2客户端ID凭据。您无法将文件上载到“与我共享”文件夹。这是一个文件夹,表示(由其他人)与您共享的一组文件。
....
C:\Data\Users\DefaultAccount\AppData\Local\Packages\e50d180b-3aea-4119-a4ff-72592c8ac7b5_4j4qdv46jwqvm\LocalState\Environmental_Data_6.txt
Lines ready to write: 1 | Lines ready to store: 1
** ERROR ** File Environmental_Data_5.txt could not be uploaded.
Lines ready to write: 2 | Lines ready to store: 2
18.9,10.0,2/16/2019 7:32:16 AM
Lines ready to write: 1 | Lines ready to store: 3
Lines ready to write: 2 | Lines ready to store: 4
18.9,9.9,2/16/2019 7:32:19 AM
C:\Data\Users\DefaultAccount\AppData\Local\Packages\e50d180b-3aea-4119-a4ff-72592c8ac7b5_4j4qdv46jwqvm\LocalState\Environmental_Data_7.txt
** ERROR ** File Environmental_Data_6.txt could not be uploaded.
Lines ready to write: 1 | Lines ready to store: 1
Lines ready to write: 2 | Lines ready to store: 2
18.9,10.1,2/16/2019 7:32:22 AM
Lines ready to write: 1 | Lines ready to store: 3
Lines ready to write: 2 | Lines ready to store: 4
The thread 0xfac has exited with code 0 (0x0).
Lines ready to write: 1 | Lines ready to store: 1
18.9,10.05,2/16/2019 7:32:25 AM
C:\Data\Users\DefaultAccount\AppData\Local\Packages\e50d180b-3aea-4119-a4ff-72592c8ac7b5_4j4qdv46jwqvm\LocalState\Environmental_Data_8.txt
** ERROR ** File Environmental_Data_7.txt could not be uploaded.
Lines ready to write: 2 | Lines ready to store: 2
...