Skip to content

Instantly share code, notes, and snippets.

@DanTup
Created September 6, 2012 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanTup/3658795 to your computer and use it in GitHub Desktop.
Save DanTup/3658795 to your computer and use it in GitHub Desktop.
Sample use of the Google+ API
using DotNetOpenAuth.OAuth2;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Plus.v1;
using Google.Apis.Requests;
using Google.Apis.Util;
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
namespace GooglePlusApiTests
{
class Program
{
const string RefreshToken = null; // Put RefreshToken here if you have got one
static void Main(string[] args)
{
var stuff = DoStuff();
stuff.Wait();
}
static async Task DoStuff()
{
var CLIENT_ID = "BLAH";
var CLIENT_SECRET = "BLAH";
// Register the authenticator and create the service
var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description, CLIENT_ID, CLIENT_SECRET);
var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);
var plusService = new PlusService(auth);
// Get the authenticated user and output their name
var me = await plusService.People.Get("me").FetchTaskAsync();
Console.WriteLine(me.DisplayName);
// Get first page of public posts for the authenticated user
var posts = await plusService.Activities.List("me", ActivitiesResource.Collection.Public).FetchTaskAsync();
foreach (var post in posts.Items)
{
// Fetch the comments for this post
var comments = await plusService.Comments.List(post.Id).FetchTaskAsync();
Console.WriteLine(post.Title);
Console.WriteLine(post.Object.Content);
Console.WriteLine(" " + comments.Items.Count + " Comments");
Console.WriteLine();
//break;
}
}
private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
{
// Get the auth URL:
IAuthorizationState state = new AuthorizationState(new[] { PlusService.Scopes.PlusMe.GetStringValue() });
state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
// If we already have a RefreshToken, use that
if (!string.IsNullOrEmpty(RefreshToken))
{
state.RefreshToken = RefreshToken;
if (arg.RefreshToken(state))
return state;
}
Uri authUri = arg.RequestUserAuthorization(state);
// Request authorization from the user (by opening a browser window):
Process.Start(authUri.ToString());
Console.Write(" Authorization Code: ");
string authCode = Console.ReadLine();
Console.WriteLine();
// Retrieve the access token by using the authorization code:
var result = arg.ProcessUserAuthorization(authCode, state);
// Output the RefreshToken we can use next time
Console.WriteLine("RT: " + state.RefreshToken);
return result;
}
}
static class ServiceRequestExtensions
{
public static Task<TResponse> FetchTaskAsync<TResponse>(this ServiceRequest<TResponse> request)
{
return Task.Factory.FromAsync<TResponse>(request.BeginFetch, request.EndFetch, null);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment