Nemiro.OAuth OAuthBase
Nemiro.OAuth OAuth2Client
Nemiro.OAuth.Clients GoogleClient
Namespace: Nemiro.OAuth.Clients
Assembly: Nemiro.OAuth (in Nemiro.OAuth.dll) Version: 1.9.4.725 (1.9.4.725)
The GoogleClient type exposes the following members.
Name | Description | |
---|---|---|
![]() | GoogleClient |
Initializes a new instance of the GoogleClient.
|
Name | Description | |
---|---|---|
![]() | Clone |
Creates a shallow copy of the current object.
(Inherited from OAuthBase.) |
![]() | Clone(NameValueCollection, String) |
Creates a shallow copy of the current object.
(Inherited from OAuthBase.) |
![]() | Equals | (Inherited from Object.) |
![]() | Finalize | (Inherited from Object.) |
![]() | GetAccessToken |
Gets the access token from the remote server.
(Inherited from OAuth2Client.) |
![]() | GetHashCode | (Inherited from Object.) |
![]() | GetSpecifiedTokenOrCurrent |
Returns the specified access token or the current access token.
(Inherited from OAuthBase.) |
![]() | GetType | (Inherited from Object.) |
![]() | GetUserInfo | Obsolete.
Gets the user details via API of the provider.
(Inherited from OAuthBase.) |
![]() | GetUserInfo(AccessToken) |
Gets the user details.
(Overrides OAuthBase GetUserInfo(AccessToken).) |
![]() | MemberwiseClone | (Inherited from Object.) |
![]() | RedirectToAuthorization |
Redirects a client to the Authorization URL.
(Inherited from OAuthBase.) |
![]() | RefreshToken |
Sends a request to refresh the access token.
(Overrides OAuth2Client RefreshToken(AccessToken).) |
![]() | RevokeToken |
Sends a request to revoke the access token.
(Overrides OAuthBase RevokeToken(AccessToken).) |
![]() | ToString | (Inherited from Object.) |
Name | Description | |
---|---|---|
![]() | AccessToken |
Gets or sets an access token.
(Inherited from OAuthBase.) |
![]() | AccessTokenUrl |
Gets or sets the address for the access token.
(Inherited from OAuthBase.) |
![]() | AccessTokenValue |
Gets an access token value.
(Inherited from OAuthBase.) |
![]() | ApplicationId |
Gets or sets the application identifier.
(Inherited from OAuthBase.) |
![]() | ApplicationSecret |
Gets or sets the application secret key.
(Inherited from OAuthBase.) |
![]() | AuthorizationCode |
Gets or sets access code for access token requests.
(Inherited from OAuthBase.) |
![]() | AuthorizationUrl |
Gets the endpoint of the authorization.
(Inherited from OAuth2Client.) |
![]() | AuthorizeUrl |
Gets or sets the base address for login.
(Inherited from OAuthBase.) |
![]() | DefaultScope |
The deault scope.
(Inherited from OAuth2Client.) |
![]() | GrantType |
Gets or sets grant type.
(Inherited from OAuth2Client.) |
![]() | Parameters |
Gets or sets additional query parameters.
(Inherited from OAuthBase.) |
![]() | Password |
Gets or sets password if GrantType is password or client_credentials.
(Inherited from OAuth2Client.) |
![]() | ProviderName |
Unique provider name: Google.
(Overrides OAuthBase ProviderName.) |
![]() | ReturnUrl |
Return URL.
(Overrides OAuthBase ReturnUrl.) |
![]() | Scope |
The scope of the access request.
(Inherited from OAuth2Client.) |
![]() | ScopeSeparator |
The separator in the scope list.
(Inherited from OAuth2Client.) |
![]() | State |
Gets or sets unique request identifier.
For clients the value sets is automatically.
(Inherited from OAuthBase.) |
![]() | SupportRefreshToken |
Gets or sets a value indicating whether the current client supports refreshing access token.
(Inherited from OAuthBase.) |
![]() | SupportRevokeToken |
Gets or sets a value indicating whether the current client supports revoking access token.
(Inherited from OAuthBase.) |
![]() | Username |
Gets or sets username if GrantType is password or client_credentials.
(Inherited from OAuth2Client.) |
![]() | Version |
Gets the version of the OAuth protocol.
(Inherited from OAuthBase.) |
Register and Configure a Google Application
![]() | Web Management Interface may change over time. Applications registration shown below may differ. If the interface is changed, you need to register the application and get Client ID and Client Secret. For web projects, configure return URLs. If you have any problems with this, please visit issues. If you do not find a solution to your problem, you can create a new question. |
Open the Google Developers Console and Create Project.
Enter the project name and click the Create.
Click to the Credential menu in the APIs & OAuth.
For desktop application, click the Create new Client ID, select Installed application and Other.
Click the Create Client ID to complete.
You will get the Client ID and Client Secret. Use this for creating an instance of the GoogleClient.
OAuthManager.RegisterClient ( new GoogleClient ( "1058655871432-83b9micke7cll89jfmcno5nftha3e95o.apps.googleusercontent.com", "AeEbEGQqoKgOZb41JUVLvEJL" ) );
For web projects create another Client ID. In the form select the Web application and specify return addresses.
For more details, please visit Google Developers Console Help.
The following example shows how to use the GoogleClient in Console Applications.
For desktop applications, the user will need to manually enter authorization code.
class Program { static void Main(string[] args) { try { var google = new GoogleClient ( "1058655871432-83b9micke7cll89jfmcno5nftha3e95o.apps.googleusercontent.com", "AeEbEGQqoKgOZb41JUVLvEJL" ); // open the login page in browser System.Diagnostics.Process.Start(google.AuthorizationUrl); // waiting of entering the access code string code = ""; while (String.IsNullOrEmpty(code)) { Console.WriteLine("Enter access code:"); code = Console.ReadLine(); } Console.WriteLine(); // set authorization code google.AuthorizationCode = code; // get user info var user = google.GetUserInfo(); Console.WriteLine("User ID: {0}", user.UserId); Console.WriteLine("Name: {0}", user.DisplayName); Console.WriteLine("Email: {0}", user.Email); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.ReadKey(); } }
Result of the program is shown in the images below.
In a web projects you can use the OAuthManager and OAuthWeb.
The following example shows how use the GoogleClient in ASP .NET MVC Application.
In the Application_Start event handler (Global.asax file) is registered the GoogleClient.
protected void Application_Start() { OAuthManager.RegisterClient ( new GoogleClient ( "1058655871432-fscjqht7ou30a75gjkde1eu1brsvbqkn.apps.googleusercontent.com", "SI5bIZkrSB5rO03YF-CdsCJC" ) ); }
The GoogleLoginResult method will handle authorization result.
public ActionResult GoogleLoginResult() { var result = OAuthWeb.VerifyAuthorization(); if (result.IsSuccessfully) { var user = result.UserInfo; // NOTE: For StringBuilder import the System.Text StringBuilder r = new StringBuilder(); r.AppendFormat("User ID: {0}\r\n", user.UserId); r.AppendFormat("Name: {0}\r\n", user.DisplayName); r.AppendFormat("Email: {0}", user.Email); return new ContentResult { Content = r.ToString(), ContentType = "text/plain" }; } return new ContentResult { Content = "Error: " + result.ErrorInfo.Message, ContentType = "text/plain" }; }
Add action method for redirection to the Google.
public ActionResult GoogleLogin() { string authUrl = OAuthWeb.GetAuthorizationUrl("Google", Url.Action("GoogleLoginResult", "Home", null, null, Request.Url.Host)); return Redirect(authUrl); }
On a page add link to the GoogleLogin method.
@Html.ActionLink("Log in with Google", "GoogleLogin")