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

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.

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).

Sharing Moments in GooglePlus from C#

Are there any libraries out there for C# that wrap the process of sharing moments to a user's Google+ account (or to their stream)? I'm looking for something that simply take your ClientId and ClientSecret, and maybe your apiKey along with the user's id to send some text that the user has decided to share with his/her friends.
If not, but you have an example of creating a WebRequest to accomplish the same thing, that would be much appreciated too!
I've reviewed this landing page: https://developers.google.com/+/quickstart/csharp
But I'm trying to integrate into an existing MVC5 application that already has the Auth for GooglePlus taken care of.
The correct client to be using for Google APIs is the Google .NET API Client library, available via NuGet. Additional libraries for specific APIs are required if you use more than the core library. For Plus, you need the Google.Apis.Plus.v1 package.
After you have added it to your projects and have configured an API client, writing app activities is as easy as:
/// <summary>The app activity type for ADD.</summary>
private const string ADD_ACTIVITY_TYPE = #"http://schemas.google.com/AddActivity";
// Construct your Plus Service, I'll assume a helper for here.
PlusService plusService = GetPlusService(credentials);
Moment addMoment = new Moment();
ItemScope target = new ItemScope()
{
Url = ContentUrl
};
addMoment.Type = ADD_ACTIVITY_TYPE;
addMoment.Target = target;
Moment response = null;
try
{
response = plusService.Moments.Insert(addMoment, "me",
MomentsResource.InsertRequest.CollectionEnum.Vault).Execute();
}
catch (System.AggregateException)
{
/* Occurs when the server can't be seen by Google. */
}
catch (Google.GoogleApiException)
{
/* Occurs when the server can't be seen by Google. */
}
How to authenticate a user and authorize your client for access to Google APIs in MVC can be found on this blog: ASP.NET MVC with OpenID and OAuth.
A final note, app activities require you to specify an app activities pseudo-scope (request_visible_actions) which is easier with the Sign-In button than via the framework. If you are getting 401 errors, this is the most likely culprit.

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.

Google AuthenticatorFactory replacement?

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!

Categories