diff options
author | András Fuchs <andras.fuchs@gmail.com> | 2013-05-26 07:54:53 -0700 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2013-05-26 07:54:53 -0700 |
commit | 2a4da5e544a22d30d5b54a2696194d814d9a442f (patch) | |
tree | ed76c7421e9669bbf263381139a29afb8eb23433 /samples/DotNetOpenAuth.ApplicationBlock/OAuth2/Google/GoogleClient.cs | |
parent | 85c21ae12f04cc50a0478cf69c82d441da23d002 (diff) | |
download | DotNetOpenAuth-2a4da5e544a22d30d5b54a2696194d814d9a442f.zip DotNetOpenAuth-2a4da5e544a22d30d5b54a2696194d814d9a442f.tar.gz DotNetOpenAuth-2a4da5e544a22d30d5b54a2696194d814d9a442f.tar.bz2 |
Samples improvements
The part which I needed
to improve is the ApplicationBlock where I changed the OAuth2 classes'
structure a little and extended them with a lot of useful
functionality, like adding many Facebook and WindowsLive scopes,
fields, structures including the asked-by-many easy to use birthdate
and avatar url getters. I have also implemented the Google OAuth2
authentication and created one common interface for all 3 Graphs in
the code (which has the common properties like Id, FirstName,
LastName, etc.), so the authentication code became really simple if
you use my version of your ApplicationBlock.
Diffstat (limited to 'samples/DotNetOpenAuth.ApplicationBlock/OAuth2/Google/GoogleClient.cs')
-rw-r--r-- | samples/DotNetOpenAuth.ApplicationBlock/OAuth2/Google/GoogleClient.cs | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/samples/DotNetOpenAuth.ApplicationBlock/OAuth2/Google/GoogleClient.cs b/samples/DotNetOpenAuth.ApplicationBlock/OAuth2/Google/GoogleClient.cs new file mode 100644 index 0000000..c623d2f --- /dev/null +++ b/samples/DotNetOpenAuth.ApplicationBlock/OAuth2/Google/GoogleClient.cs @@ -0,0 +1,107 @@ +//----------------------------------------------------------------------- +// <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 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 const string PlusMe = "https://www.googleapis.com/auth/plus.me"; + + 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"; + } + } + } +} |