Reputation: 43
I'm trying to upload a file in Google Drive using v3 API, but i don't understand how to get access.
I have aleady the token obtained by OAuth2 (I make a request for grant access to Google Drive of user and obtain tokens)
There is my code, but I don't understand how put the token for the authentication.
//creazione servizio
var DriveService = new Google.Apis.Drive.v3.DriveService();
//creazione del file
Google.Apis.Drive.v3.Data.File body = new Google.Apis.Drive.v3.Data.File();
body.Name = nome;
body.MimeType = mimeType;
//upload del file
Google.Apis.Drive.v3.FilesResource.CreateMediaUpload request = DriveService.Files.Create(body,file,mimeType);
request.OauthToken = token;
request.Fields = "id";
request.Upload();
Response.Write(request.Upload().Exception.Message);
var returnFile = request.ResponseBody;
if (returnFile != null)
{
esito.urlFile = returnFile.Id;
esito.esito = true;
}
Thanks in advance to everybody.
Upvotes: 1
Views: 936
Reputation: 8082
Invalid Credentials, which you have indicated in your SO post title, is usually encountered if the access token you're using is either expired or invalid as stated in Handling API Errors.
Suggested action for such error is to refresh the access token using the long-lived refresh token. If this fails, direct the user through the OAuth flow, as described in Authorizing Your App with Google Drive.
I find it helpful and you will understand better about Authentication and Upload Files if you go through these tutorials:
Hope those tutorials help. :)
Upvotes: 1