Gdrive无法下载而无法上传

时间:2015-02-16 19:59:19

标签: google-drive-api

我正在使用GDRive insertFile和RevtrieveAllFiles上传和下载文件到googledrive。 我创建了一个客户端密钥和Id并修改了凭据。 代码基于代码项目http://www.codeproject.com/KB/WPF/488185/GDrive_Uploader_Sample.zip

中的帖子

但它无法调试Utilities.InsertFile 在file.upload中,它失败,exceptionvalue不能为null - uriString。 在下载中,它在FileList files = request.Fetch();中失败

 // First, create a reference to the service you wish to use.
    // For this app, it will be the Drive service. But it could be Tasks, Calendar, etc.
    // The CreateAuthenticator method is passed to the service which will use that when it is time to authenticate
    // the calls going to the service.
    _service = new DriveService(CreateAuthenticator());

    // Open a dialog box for the user to pick a file.
    OpenFileDialog dialog = new OpenFileDialog();
    dialog.AddExtension = true;
    dialog.DefaultExt = ".txt";
    dialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
    dialog.Multiselect = false;
    dialog.ShowDialog();


    File body = new File();
    body.Title = System.IO.Path.GetFileName(dialog.FileName);
    body.Description = "A test document";
    body.MimeType = "text/plain";

    System.IO.Stream fileStream = dialog.OpenFile();
    byte[] byteArray = new byte[fileStream.Length];
    fileStream.Read(byteArray, 0, (int)fileStream.Length);

    System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);

    // Get a listing of the existing files...
    List<File> fileList = Utilities.RetrieveAllFiles(_service);

    // Set a flag to keep track of whether the file already exists in the drive
    bool fileExists = false;

    foreach (File item in fileList)
    {
        if (item.Title == body.Title)
        {
            // File exists in the drive already!
            fileExists = true;
            MessageBoxResult result = System.Windows.MessageBox.Show("The file you picked already exists in your Google Drive. Do you wish to overwrite it?", "Confirmation", MessageBoxButton.YesNoCancel);
            if (result == MessageBoxResult.Yes)
            {

                // Yes... overwrite the file
                Utilities.UpdateFile(_service, item.Id, item.Title, item.Description, item.MimeType, dialog.FileName, true);
                List<File> allFiles = Utilities.RetrieveAllFiles(_service);

            }

            else if (result == MessageBoxResult.No)
            {

                // MessageBoxResult.No code here
              File f=             Utilities.insertFile(_service, System.IO.Path.GetFileName(dialog.FileName), "An uploaded document", "", "text/plain", dialog.FileName);
            }

            else
            {

                // MessageBoxResult.Cancel code here
                return;
            }
            break;
        }
    }

    // Check to see if the file existed. If not, it is a new file and must be uploaded.
    if (!fileExists)
    {
      File file=  Utilities.insertFile(_service, System.IO.Path.GetFileName(dialog.FileName), "An uploaded document", "", "text/plain", dialog.FileName);
      var list = Utilities.RetrieveAllFiles(_service);

    }

    System.Windows.MessageBox.Show("Upload Complete");

1 个答案:

答案 0 :(得分:0)

打开gdrive并将sdk设置为On。这将使应用程序能够访问gdrive帐户