diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2013-05-31 08:56:40 -0700 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2013-05-31 08:56:40 -0700 |
commit | acdf7421c0e51a2c7cb50ae3278769e4c295783d (patch) | |
tree | c16be1dc23d2938df927811bad274dab86323fb8 /samples/DotNetOpenAuth.ApplicationBlock/OAuth2/Google | |
parent | d8a034db629baa0e2a86528eac5d0ab49d9bb984 (diff) | |
parent | 3858496786be3349e5c7958e3d22766e4883e488 (diff) | |
download | DotNetOpenAuth-acdf7421c0e51a2c7cb50ae3278769e4c295783d.zip DotNetOpenAuth-acdf7421c0e51a2c7cb50ae3278769e4c295783d.tar.gz DotNetOpenAuth-acdf7421c0e51a2c7cb50ae3278769e4c295783d.tar.bz2 |
Merge branch 'v4.3' of https://github.com/DotNetOpenAuth/DotNetOpenAuth into v4.3
Diffstat (limited to 'samples/DotNetOpenAuth.ApplicationBlock/OAuth2/Google')
-rw-r--r-- | samples/DotNetOpenAuth.ApplicationBlock/OAuth2/Google/GoogleClient.cs | 110 | ||||
-rw-r--r-- | samples/DotNetOpenAuth.ApplicationBlock/OAuth2/Google/GoogleGraph.cs | 152 |
2 files changed, 262 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..1e1a486 --- /dev/null +++ b/samples/DotNetOpenAuth.ApplicationBlock/OAuth2/Google/GoogleClient.cs @@ -0,0 +1,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"; + } + } + } +} diff --git a/samples/DotNetOpenAuth.ApplicationBlock/OAuth2/Google/GoogleGraph.cs b/samples/DotNetOpenAuth.ApplicationBlock/OAuth2/Google/GoogleGraph.cs new file mode 100644 index 0000000..44adcaf --- /dev/null +++ b/samples/DotNetOpenAuth.ApplicationBlock/OAuth2/Google/GoogleGraph.cs @@ -0,0 +1,152 @@ +//----------------------------------------------------------------------- +// <copyright file="GoogleGraph.cs" company="Andras Fuchs"> +// 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.Runtime.Serialization; + using System.Runtime.Serialization.Json; + using System.Text; + + //// Documentation: https://developers.google.com/accounts/docs/OAuth2Login + + [DataContract] + public class GoogleGraph : IOAuth2Graph { + private static DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(GoogleGraph)); + + /// <summary> + /// Gets or sets the value of this field is an immutable identifier for the logged-in user, and may be used when creating and managing user sessions in your application. This identifier is the same regardless of the client_id. This provides the ability to correlate profile information across multiple applications in the same organization. The value of this field is the same as the value of the userid field returned by the TokenInfo endpoint. + /// </summary> + [DataMember(Name = "id", IsRequired = true)] + public string Id { get; set; } + + /// <summary> + /// Gets or sets the email address of the logged in user + /// </summary> + [DataMember(Name = "email")] + public string Email { get; set; } + + /// <summary> + /// Gets or sets a flag that indicates whether or not Google has been able to verify the email address. + /// </summary> + [DataMember(Name = "verified_email")] + public bool? VerifiedEmail { get; set; } + + /// <summary> + /// Gets or sets the full name of the logged in user + /// </summary> + [DataMember(Name = "name", IsRequired = true)] + public string Name { get; set; } + + /// <summary> + /// Gets or sets the first name of the logged in user + /// </summary> + [DataMember(Name = "given_name")] + public string GivenName { get; set; } + + /// <summary> + /// Gets or sets the last name of the logged in user + /// </summary> + [DataMember(Name = "family_name")] + public string FamilyName { get; set; } + + /// <summary> + /// Gets or sets the URL to the user's profile picture. If the user has no public profile, this field is not included. + /// </summary> + [DataMember(Name = "picture")] + public Uri Picture { get; set; } + + /// <summary> + /// Gets or sets the user's registered locale. If the user has no public profile, this field is not included. + /// </summary> + [DataMember(Name = "locale")] + public string Locale { get; set; } + + /// <summary> + /// Gets or sets the default timezone of the logged in user + /// </summary> + [DataMember(Name = "timezone")] + public string Timezone { get; set; } + + /// <summary> + /// Gets or sets the gender of the logged in user (other|female|male) + /// </summary> + [DataMember(Name = "gender")] + public string Gender { get; set; } + + [DataMember(Name = "birthday")] + public string Birthday { get; set; } + + [DataMember(Name = "link")] + public Uri Link { get; set; } + + public Uri AvatarUrl { + get { + return this.Picture; + } + } + + public DateTime? BirthdayDT { + get { + if (!string.IsNullOrEmpty(this.Birthday) && (!this.Birthday.StartsWith("0000"))) { + return DateTime.ParseExact(this.Birthday, "yyyy-MM-dd", null); + } + + return null; + } + } + + public HumanGender GenderEnum { + get { + if (this.Gender == "male") { + return HumanGender.Male; + } else if (this.Gender == "female") { + return HumanGender.Female; + } else if (this.Gender == "other") { + return HumanGender.Other; + } + + return HumanGender.Unknown; + } + } + + public string FirstName { + get { + return this.GivenName; + } + } + + public string LastName { + get { + return this.FamilyName; + } + } + + public string UpdatedTime { + get { + return null; + } + } + + public static GoogleGraph Deserialize(string json) { + if (string.IsNullOrEmpty(json)) { + throw new ArgumentNullException("json"); + } + + return Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(json))); + } + + public static GoogleGraph Deserialize(Stream jsonStream) { + if (jsonStream == null) { + throw new ArgumentNullException("jsonStream"); + } + + return (GoogleGraph)jsonSerializer.ReadObject(jsonStream); + } + } +} |