Skip to content

Commit 5580826

Browse files
Merge pull request #233 from DharanitharanA/main
908992-Added opening and saving Word document in Google Drive cloud storage samples
2 parents 5837d4b + 22bad9b commit 5580826

File tree

10 files changed

+416
-0
lines changed

10 files changed

+416
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.8.34205.153
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Open-Word-document", "Open-Word-document\Open-Word-document.csproj", "{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {024A6C01-116E-4483-9B82-338F6A791749}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Open_Word_document</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Google.Apis.Drive.v3" Version="1.62.0.3155" />
13+
</ItemGroup>
14+
</Project>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Google.Apis.Auth.OAuth2;
2+
using Google.Apis.Drive.v3;
3+
using Google.Apis.Services;
4+
using Google.Apis.Util.Store;
5+
6+
namespace Open_Word_document
7+
{
8+
internal class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
UserCredential credential;
13+
string[] Scopes = { DriveService.Scope.DriveReadonly };
14+
string ApplicationName = "YourAppName";
15+
// Step 1: Open Google Drive with credentials.
16+
using (var stream1 = new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
17+
{
18+
string credPath = "token.json";
19+
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
20+
GoogleClientSecrets.Load(stream1).Secrets,
21+
Scopes,
22+
"users",
23+
CancellationToken.None,
24+
new FileDataStore(credPath, true)).Result;
25+
}
26+
27+
// Step 2: Create Drive API service.
28+
var service = new DriveService(new BaseClientService.Initializer()
29+
{
30+
HttpClientInitializer = credential,
31+
ApplicationName = ApplicationName,
32+
});
33+
34+
// Step 3: Specify the file ID of the Word document you want to open.
35+
string fileId = "YOUR_FILE_ID"; // Replace with the actual file ID YOUR_FILE_ID.
36+
37+
// Step 4: Download the Word document from Google Drive.
38+
var request = service.Files.Get(fileId);
39+
var stream = new MemoryStream();
40+
request.Download(stream);
41+
42+
// Step 5: Save the Word document locally
43+
using (FileStream fileStream = new FileStream("Output.docx", FileMode.Create, FileAccess.Write))
44+
{
45+
stream.WriteTo(fileStream);
46+
}
47+
//Dispose the stream.
48+
stream.Dispose();
49+
}
50+
}
51+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.8.34205.153
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Save-Word-document", "Save-Word-document\Save-Word-document.csproj", "{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{93F4EA0D-FA3D-47FB-8DFC-204C609E856F}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {024A6C01-116E-4483-9B82-338F6A791749}
24+
EndGlobalSection
25+
EndGlobal
78.9 KB
Loading
20.2 KB
Loading
18.5 KB
Loading
19.6 KB
Loading

0 commit comments

Comments
 (0)