summaryrefslogtreecommitdiffstats
path: root/samples/OAuthClient/Google.aspx.cs
blob: fda643e3153a3ecaf9a64a165243e1a5a70c32ba (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
namespace OAuthClient
{
	using System;
	using System.Configuration;
	using System.Net;
	using System.Web;
	using DotNetOpenAuth.ApplicationBlock;
	using DotNetOpenAuth.OAuth2;

	public partial class Google : System.Web.UI.Page
	{
		private static readonly GoogleClient googleClient = new GoogleClient
		{
			ClientIdentifier = ConfigurationManager.AppSettings["googleClientID"],
			ClientCredentialApplicator = ClientCredentialApplicator.PostParameter(ConfigurationManager.AppSettings["googleClientSecret"]),
		};

		protected void Page_Load(object sender, EventArgs e)
		{
			IAuthorizationState authorization = googleClient.ProcessUserAuthorization();
			if (authorization == null)
			{
				// Kick off authorization request
				googleClient.RequestUserAuthorization();

				// alternatively you can ask for more information
				// googleClient.RequestUserAuthorization(scope: new[] { GoogleClient.Scopes.UserInfo.Profile, GoogleClient.Scopes.UserInfo.Email });
			}
			else
			{
				IOAuth2Graph oauth2Graph = googleClient.GetGraph(authorization);

				this.nameLabel.Text = HttpUtility.HtmlEncode(oauth2Graph.Name);
			}
		}
	}
}