Console application in .NET which call the google coordinates api to get
the list of jobs
Following is my code :
using System;
using System.Diagnostics;
using System.Collections.Generic;
using DotNetOpenAuth.OAuth2;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Coordinate.v1;
using Google.Apis.Coordinate.v1.Data;
using Google.Apis.Services;
//using Google.Apis.Util;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net;
using System.IO;
namespace GoogleCoordinateConsole
{
class Program
{
static void Main(string[] args)
{
try
{
String TEAM_ID = "itJAYgFgYEXVxpSszflw_Q";
//string coordinateURL;
//string authRequestURL;
//string clientID =
"715909815178.apps.googleusercontent.com";
//string redirect_uri = "http://www.risecorp.com";
//string scope =
"https://www.googleapis.com/auth/coordinate";
//authRequestURL =
"https://accounts.google.com/o/oauth2/auth?client_id=" +
clientID + "&redirect_uri=" + redirect_uri + "&scope=" +
scope + "&response_type=token";
//coordinateURL =
"https://www.googleapis.com/coordinate/v1/teams/" +
TEAM_ID +
"/jobs?key=AIzaSyBKzp6sFF5oF8iQNjeICas8WecpbG0COC0";
//WebRequest getRequestURL;
//getRequestURL = WebRequest.Create(authRequestURL);
//Stream respStream;
//respStream =
getRequestURL.GetResponse().GetResponseStream();
//WebRequest getURL;
//getURL = WebRequest.Create(coordinateURL);
//Stream objStream;
//objStream = getURL.GetResponse().GetResponseStream();
//StreamReader objReader = new StreamReader(objStream);
//string sLine = "";
//int i = 0;
//while (sLine != null)
//{
// i++;
// sLine = objReader.ReadLine();
// if (sLine != null)
// Console.WriteLine("{0}:{1}", i, sLine);
//}
//Console.ReadLine();
var provider = new
WebServerClient(GoogleAuthenticationServer.Description);
provider.ClientIdentifier =
"1072149943082.apps.googleusercontent.com";
provider.ClientSecret = "8ZXTUTyJPipUIRdOtTKwTFfH";
var auth = new
OAuth2Authenticator<WebServerClient>(provider,
GetAuthorization);
//var service = new CoordinateService();
//service.Authenticator = auth;
var service = new CoordinateService(new
BaseClientService.Initializer()
{
Authenticator = auth
});
JobsResource.ListRequest lists = service.Jobs.List(TEAM_ID);
string numberofJobs = lists.TeamId;
JobListResponse jobListResponse = lists.Execute();
//JobsResource.InsertRequest ins =
service.Jobs.Insert(null, TEAM_ID, "My Home", 51, 0,
"Created this Job with the .Net Client Library");
Console.WriteLine("Total Jobs:");
Console.WriteLine(numberofJobs.ToString());
Console.WriteLine("Press any Key to Continue");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine("Error is :" + ex);
Console.WriteLine("Press any Key to Continue");
Console.ReadKey();
}
}
private static IAuthorizationState
GetAuthorization(WebServerClient client)
{
IAuthorizationState state = new AuthorizationState(new[] {
"https://www.googleapis.com/auth/coordinate" });
// The refresh token has already been retrieved offline
// In a real-world application, this has to be stored
securely, since this token
// gives access to all user data on the Coordinate scope, for
the user who accepted the OAuth2 flow
// TO UPDATE (see below the sample for instructions)
state.RefreshToken =
"1/F-Gedu3D8KkLMz5Jv8dD-og0N383gsaJHouJcTaClEo";
return state;
}
}
}
While executing this i am getting error:
DotNetOpenAuth.Messaging.ProtocolException: Error occurred while sending a
direct message or getting the response. ---> System.Net.WebException: The
remote server returned an error: (400) Bad Request.
Any clue what could be the possible error?
No comments:
Post a Comment