Reputation: 356
I am trying to implement OAuth Google API, but did not find any elegnat example to start with. Somehow, I statred with http://dotnetopenauth.net/
I created a single page named "Login.aspx" which contain the code
using DotNetOpenAuth.OpenId.Extensions.AttributeExchange;
using DotNetOpenAuth.OpenId.RelyingParty;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace GoogleOAuth
{
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var openid = new OpenIdRelyingParty();
var response = openid.GetResponse();
//Checks whether user is authenticated or not
if (response != null && response.Status == AuthenticationStatus.Authenticated && response.Provider.Uri == new Uri("https://www.google.com/accounts/o8/ud"))
{
var fetch = response.GetExtension<FetchResponse>();
string email = String.Empty;
if (fetch != null)
{
email = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email); //Fetching requested emailid
}
}
}
protected void GoogleButtonClick(object sender, EventArgs e)
{
using (var openid = new OpenIdRelyingParty())
{
var request = openid.CreateRequest("https://www.google.com/accounts/o8/id");
var fetch = new FetchRequest();
fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email); // Request for email id
request.AddExtension(fetch); // Adding in request obj
request.RedirectToProvider();
}
}
}
}
On this page, it works fine. After ogin the above code return the email address. But what I am wondering to transfer the user after he logged in and fetch data on a different page instead on the same page. How it is possible?
If I copied the code which is inside Page_Load, and paste into other page it does not work. That return null in response.
Please suggest
Upvotes: 1
Views: 469
Reputation: 116868
I recomend that you start by using C# using Google.Apis.v3. Depending upon which api you are trying to access things will be slightly diffrent.
UserCredential credential;
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets { ClientId = "YourClientId", ClientSecret = "YourClientSecret" },
new[] { DriveService.Scope.Drive,
DriveService.Scope.DriveFile },
"user",
CancellationToken.None,
new FileDataStore("Drive.Auth.Store")).Result; }
Thats an Oauth example that will connect to Google Drive. From the looks of your code you are trying to access google+ so the Scopes will be diffrent.
You can find a basic tutorial to get you started with Google Oath2 here: Google OAuth2 C#
Upvotes: 1