Google AuthenticatorFactory replacement? - c#

The 'First App' tutorial for Google Calendar API uses AuthenticatorFactory when authenticating the application with Google. According to this link, AuthenticatorFactory was removed from the API on Aug 8, 2011. Can anyone point me in the direction to find the 'new' way to authenticate my application with Google?
EDIT: Just realized that I didn't include the language I'm using - I'm looking for examples on how to authenticate/authorize an installed C# application.

Sorry for opening an old topic, but I came accross the same question and it took me quite some time to find the solution. So for all the people with the same problem:
AuthenticatorFactory won't work, you need to create your service like with the following code:
var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthentication);
// Create the service.
var service = new CalendarService(new BaseClientService.Initializer
{
Authenticator = auth
});
Good luck!

Related

Migrate web forms ASP.NET Membership OpenAuth for Google from OpenID 2.0 to OpenID Connect

I have an existing web forms site that utilizes the built-in ASP.NET Membership OpenAuth model. All has worked well for over a year and a half, but I just found out that Google has deprecated OpenID 2.0 and will shut it down on April 20, 2015 (just over a week).
This is where abstraction really bites you in the ass, because all I have is a single line of code in my AuthConfig.cs
OpenAuth.AuthenticationClients.AddGoogle();
I've updated all of my OpenAuth related NuGet packages, hoping that there was a fix for this under the covers - but no joy.
Is there an easy solution for this, or do I need to completely strip out and rewrite the Google authentication piece?
(Lots of questions related to this, but I haven't found anything specifically related to the web forms Membership.OpenAuth implementation.)
The Nuget package "DotNetOpenAuth OAuth2 Client for Google" solved this for me.
In AuthConfig.cs, replace OpenAuth.AuthenticationClients.AddGoogle(); with:
var client = new GoogleOAuth2Client(clientID, clientSecret);
var extraData = new Dictionary<string, string>();
OpenAuth.AuthenticationClients.Add("Google", () => client, extraData);
Get the string values clientID and clientSecret by setting up your Google App.
In the page responding to the redirect Uri you register with Google, add GoogleOAuth2Client.RewriteRequest() before your existing call to OpenAuth.VerifyAuthentication(redirectUrl).

Modifying OWIN OAuth middleware to use JWT bearer tokens

I'm currently trying to create a proof of concept for claims based authentication for a new app using a combination of the following technologies: Web API 2, OWIN middleware and JWT.
To keep things simple I started with the Web API 2 project template and changed the authentication to 'Individual User Accounts'. The sample client I created was then able to get a token by calling /Token and was able to call a sample endpoint with the OAuth bearer token. So far so good. I then added the following code to Startup.Auth.cs to try and enable JwtBearerAuthentication:
var jwtOptions = new JwtBearerAuthenticationOptions
{
AllowedAudiences = audiences,
IssuerSecurityTokenProviders = new[] {
new SymmetricKeyIssuerSecurityTokenProvider(issuer, signingKey) }
};
app.UseJwtBearerAuthentication(jwtOptions);
I expected that Web API 2 would start returning JWTs from the call to /Token, but it doesn't appear to have done anything. I've been banging my head against this for a few days with no success and the Microsoft documents aren't very forthcoming.
I also tried adding the following to my OAuthAuthorizationServerOptions
AuthorizationCodeFormat = new JwtFormat(audience, new SymmetricKeyIssuerSecurityTokenProvider(issuer, signingKey))
I could also be trying to doing the completely wrong thing.
Any ideas would be greatly appreciated.
Well, now there is a setting on OAuthAuthorizationServerOptions that you can specify the format of your access token, not the authorization code, like you're doing on you example.
So, instead of:
AuthorizationCodeFormat = new JwtFormat(audience, new SymmetricKeyIssuerSecurityTokenProvider(issuer, signingKey))
You should have:
AccessTokenFormat = new JwtFormat(audience, new SymmetricKeyIssuerSecurityTokenProvider(issuer, signingKey))
The Windows Identity Foundation uses a proprietary token format, not JWT. The JWT code you see above is for consuming tokens, not generating them. There is a helpful discussion on the ASP.NET forums.
However, in the second half of 2014 Microsoft officially released support for JWT in Windows Identity foundation, with the JSON Web Token Handler. You should be able to install and use that package to solve the problem you have described.
I don't think there's any current way to override how the token is output in the response. I took a look at the OAuthAuthorizationServerHandler in the Katana source code repository.
You'll see that in the InvokeTokenEndpointAsync method, there is a section that creates a JsonTextWriter which generates the response. It is not done in such a way that any kind of extension would affect it.
I find this frustrating too. Microsoft's library should have some way to easily override the response serialization. You can't even add your own custom parameters to the response.
You can use this sample https://github.com/thinktecture/Thinktecture.IdentityModel/tree/master/samples/OAuth2/EmbeddedResourceOwnerFlow
for writting authentication logic in your project.
After it you must add [Authorize] attribute to each controller or action which requires authorization(OWIN Katana contains the logic of validating token, authorization and some other useful things).

Kinda confused on how to use Google Api. Stuck with Oauth

I am not sure if there any libraries(if anyone knows any please let me know) out there that make it easier to use googles api. I know they have classes for .net but they don't seem to work with Windows Phone 7.
I am trying to figure out how it all works but I really find Google's documentation confusion and missing lots of steps.
I decided to try to at first to use their library in an asp.net mvc application before trying to figure out how to do it all with REST requests in a WP7 app.
I am stuck at trying to figure out how to do the Oauth to validation user so I can get their contacts.
RequestSettings settings = new RequestSettings("<var>Test</var>");
settings.OAuth2Parameters = new OAuth2Parameters
{
ClientId = "",
ClientSecret = "",
};
// Need some Code here to validation user so I can then get their contacts.
ContactsRequest cr = new ContactsRequest(settings);
var c = cr.GetGroups();
There is an OAuth NuGet package (DotNetOpenAuth) available, providing authentication through Google, Twitter, Facebook, MSN and possibly more.
Maybe you have more luck with that.

Issue with posting on business page of facebook using c# sdk

I have read many many articles tonight and still do not understood what I shoud do to post on business page of facebook. Here is my code:
string token = getHtml("https://graph.facebook.com/oauth/access_token?client_id=clientID&client_secret=secretKey&grant_type=client_credentials");
token = token.Replace("access_token=", "");
FacebookClient fbClient = new FacebookClient(token);
var args = new Dictionary<string, object>();
args["message"] = "Testing 12qwe3";
fbClient.Post("/pageID/feed", args);
It always says app not authorized. How to authorize it? I read so many articles, but still not able to do it.
I am posting this after totally failing from researching many articles. Kindly let me know whats wrong?
----UPDATE
I did the first part, by making myself the manager page and then creating an app and then providing that app the ability to manage pages and publish etc...but again another issue when i do so even using the above code i still get the same error.But when i use the tools (GRAPH API EXPLORER) portion of facebook site and select this app and generate a token .I than use that directly in app and it posts fine. Kinldy let me know whats wrong!

Facebook C# SDK Fan Page Post From ASP.NET

I know this has been asked many times and I apologize if the answer is readily available but I have not been able to find the answer.
I'm using VS 2010 with the newest version of Facebook C# SDK. I installed it using the "Install-Package Facebook" command from my package manager in VS.
After spending quite a bit of time researching how to use the SDK, I am authenticating with FB through the Facebook Javascript SDK. Actually I followed the steps from the Getting Started Tutorial. And was quite successful in my endeavors. I now have a "Login" button on my aspx page, which brings up the login window and allows me to authenticate via that. I was also able to store my access token in a session variable and have it for use.
I have tested this by doing a Get on my FacebookClient object that I created with my access token and was able to retrieve data about the Fan Page.
So having said all this, in order to let you know that I have had some success. I was ready to start posting to the Fan Page via my web site and found this link Simplest way to post to a facebook fan page's wall with C#!
I thought - great! This is exactly what I need. After copying the code from this post, I am now getting an "Unknown Type" on the ExpandoObject.
For the sake of convenience, here is the code that I copied.
if (Session["AccessToken"] != null)
{
var accessToken = Session["AccessToken"].ToString();
dynamic messagePost = new ExpandoObject();
messagePost.access_token = "[YOUR_ACCESS_TOKEN]";
messagePost.picture = "[A_PICTURE]";
messagePost.link = "[SOME_LINK]";
messagePost.name = "[SOME_NAME]";
messagePost.caption = "{*actor*} " + "[YOUR_MESSAGE]"; //<---{*actor*} is the user (i.e.: Aaron)
messagePost.description = "[SOME_DESCRIPTION]";
FacebookClient app = new FacebookClient(accessToken);
app.Post("/" + [PAGE ID] + "/feed", messagePost);
}
So my questions are really about the ExpandoObject. Is this object still in the SDK? If so, what am I missing and if not, how should I be making this call?
And while I'm asking, is there something else I need to do when requesting the access token in order to get the proper permissions to post on the Fan Page? As stated above, I followed the Getting Started page on Facebook C# SDK Site to get my access token. The reason I ask is because throughout my research I have been coming across different posts talking about making sure you have the proper permissions in order to do different things in Facebook.
Any help is greatly appreciated,
Dan
I guess I just needed to do a little more poking around in StackOverflow to find my answer. I will go ahead and post it here for anyone else that might be having the same issue.
As it turns out with the latest version of Facebook C# SDK, the way to post anything is by using the FacebookClient object and then calling the Post method on that object. Pretty easy actually. Here is the code that I used.
if (Session["AccessToken"] != null)
{
var accessToken = Session["AccessToken"].ToString();
FacebookClient app = new FacebookClient(accessToken);
dynamic result = app.Post("/[ID]/feed", new Dictionary<string, object> { { "message", "This Post was made from my website" } });
}
Session["AccessToken"] is my authentication token that was obtained from the user logging into Facebook on the web page, and [ID] is the Id of my Fan Page.

Categories