[]
To configure Google Drive Web API Service and perform different operations, follow these steps:
Create Google API and get the credentials.json
file to your project. For more information on Google Drive API, see Configure your UI integration.
Create a new C1 Web API Application and make sure the following references are added to the application:
Add the "credentials.json" credential file to the project.
Install Google.Apis.Drive.v3 NuGet package from the NuGet Package Manager.
Create a method, say GetUserCredential, to get the credentials from credentials.json file using the following code:
using Google.Apis.Auth.OAuth2;
private UserCredential GetUserCredential(string[] scopes)
{
UserCredential credential;
using (var fileStream =
new FileStream(HttpContext.Current.Server.MapPath("credentials.json"), FileMode.Open, FileAccess.Read))
{
string credPath = "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(fileStream).Secrets,
scopes,
"user",
CancellationToken.None,
new FileDataStore(HttpContext.Current.Server.MapPath(credPath), true)).Result;
}
return credential;
}
Note that the file token.json stores the user's access and refreshes tokens, and is created automatically when the authorization flow completes for the first time.
Register the Google Drive cloud service using the following code:
using Google.Apis.Drive.v3;
string[] scopes = { DriveService.Scope.Drive };
string applicationName = "yout application name";
app.UseStorageProviders().AddGoogleDriveStorage("GoogleDrive", GetUserCredential(scopes), applicationName);
You can choose to perform various operations, such as listing data, uploading, deleting or downloading a file, after registering the cloud service. To understand how to perform these operations, see Perform Operations.