简体   繁体   中英

Unable To upload file to google drive - using c#

All code runs without errors, but when I check my Google Drive account I can't find the file I am uploading ("document.txt").

Also it has asked me for Authentication again.

UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
   new ClientSecrets
   {
       ClientId = "Here my clientid",
          ClientSecret = "client secret",
   },
   new[] { DriveService.Scope.Drive },
   "user",
   CancellationToken.None).Result;

// Create the service.
var service = new DriveService(new BaseClientService.Initializer()
{
    HttpClientInitializer = credential,
    ApplicationName = "Drive API Sample",
});

File body = new File();
body.Title = "My document";
body.Description = "A test document";
body.MimeType = "text/plain";

byte[] byteArray = System.IO.File.ReadAllBytes("document.txt");
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);

FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "text/plain");
request.Upload();

File file = request.ResponseBody;

Questions: Why cant I find my uploaded file, and how can I get it to remember my authentication.

I think you are forgetting body.Parent so it doesn't know what directory to place the file into.

parents[] list Collection of parent folders which contain this file. Setting this field will put the file in all of the provided folders. On insert, if no folders are provided, the file will be placed in the default root folder.

example:

body.Parents = new List<ParentReference>() { new ParentReference() { Id = 'root' } };

You are getting asked for authentication again because you aren't saving authentication.

//Scopes for use with the Google Drive API
string[] scopes = new string[] { DriveService.Scope.Drive,
                                 DriveService.Scope.DriveFile};
// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
UserCredential credential = 
            GoogleWebAuthorizationBroker
                          .AuthorizeAsync(new ClientSecrets { ClientId = CLIENT_ID
                                                            , ClientSecret = CLIENT_SECRET }
                                          ,scopes
                                          ,Environment.UserName
                                          ,CancellationToken.None
                                          ,new FileDataStore("Daimto.GoogleDrive.Auth.Store")
                                          ).Result;

FileDataStore stores the authentication data in the %appdata% directory.

More detailed information can be found in the tutorial Google Drive API with C# .net – Upload

Update For the following error:

"The API is not enabled for your project, or there is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your configuration. [403]"

Go to Developer console for your project here Under APIs & auth -> APIs enable Google drive API and sdk. Also go to credentials and make sure you added a product name and email.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM