summaryrefslogtreecommitdiffstats
path: root/samples/DotNetOpenAuth.ApplicationBlock/OAuth2/Google/GoogleClient.cs
blob: 1e1a486e0c0cd91986d932c2784ceb8c8d6b5b43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//-----------------------------------------------------------------------
// <copyright file="GoogleClient.cs" company="Outercurve Foundation">
//     Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.ApplicationBlock {
	using System;
	using System.Collections.Generic;
	using System.IO;
	using System.Linq;
	using System.Net;
	using System.Text;
	using System.Web;
	using DotNetOpenAuth.Messaging;
	using DotNetOpenAuth.OAuth2;

	//// https://accounts.google.com/o/oauth2/auth

	public class GoogleClient : WebServerClient {
		private static readonly AuthorizationServerDescription GoogleDescription = new AuthorizationServerDescription {
			TokenEndpoint = new Uri("https://accounts.google.com/o/oauth2/token"),
			AuthorizationEndpoint = new Uri("https://accounts.google.com/o/oauth2/auth"),
			//// RevokeEndpoint = new Uri("https://accounts.google.com/o/oauth2/revoke"),
			ProtocolVersion = ProtocolVersion.V20
		};

		/// <summary>
		/// Initializes a new instance of the <see cref="GoogleClient"/> class.
		/// </summary>
		public GoogleClient()
			: base(GoogleDescription) {
		}

		public IOAuth2Graph GetGraph(IAuthorizationState authState, string[] fields = null) {
			if ((authState != null) && (authState.AccessToken != null)) {
				WebRequest request = WebRequest.Create("https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + Uri.EscapeDataString(authState.AccessToken));
				WebResponse response = request.GetResponse();

				if (response != null) {
					Stream responseStream = response.GetResponseStream();

					if (responseStream != null) {
						return GoogleGraph.Deserialize(responseStream);
					}
				}
			}

			return null;
		}

		/// <summary>
		/// Well-known scopes defined by Google.
		/// </summary>
		/// <remarks>
		/// This sample includes just a few scopes.  For a complete list of permissions please refer to:
		/// https://developers.google.com/accounts/docs/OAuth2Login
		/// </remarks>
		public static class Scopes {
			public const string PlusMe = "https://www.googleapis.com/auth/plus.me";

			/// <summary>
			/// Scopes that cover queries for user data.
			/// </summary>
			public static class UserInfo {
				/// <summary>
				/// Gain read-only access to basic profile information, including a user identifier, name, profile photo, profile URL, country, language, timezone, and birthdate.
				/// </summary>
				public const string Profile = "https://www.googleapis.com/auth/userinfo.profile";

				/// <summary>
				/// Gain read-only access to the user's email address.
				/// </summary>
				public const string Email = "https://www.googleapis.com/auth/userinfo.email";
			}

			public static class Drive {
				/// <summary>
				/// Full, permissive scope to access all of a user's files. Request this scope only when it is strictly necessary.
				/// </summary>
				public const string Default = "https://www.googleapis.com/auth/drive";

				/// <summary>
				/// Per-file access to files created or opened by the app
				/// </summary>
				public const string File = "https://www.googleapis.com/auth/drive.file";

				/// <summary>
				/// Allows apps read-only access to the list of Drive apps a user has installed
				/// </summary>
				public const string AppsReadonly = "https://www.googleapis.com/auth/drive.apps.readonly";

				/// <summary>
				/// Allows read-only access to file metadata and file content
				/// </summary>
				public const string Readonly = "https://www.googleapis.com/auth/drive.readonly";

				/// <summary>
				/// Allows read-only access to file metadata, but does not allow any access to read or download file content
				/// </summary>
				public const string Metadata = "https://www.googleapis.com/auth/drive.readonly.metadata";

				/// <summary>
				/// Special scope used to let users approve installation of an app
				/// </summary>
				public const string Install = "https://www.googleapis.com/auth/drive.install";
			}
		}
	}
}