0

Visual Studio 2010 Framework 4.0

I am trying to insert a job using Google coordinate API.For inserting job I tried two ways.Using webrequest and coordinate API.But I am not able to insert the job. Using Google Coordinate API:Getting error"Authentication doesn't exist in namespace google.api"

I installed google coordinate API using nuget.I used below code for inserting job using coordinate API.But I am getting error on “GoogleAuthenticationServerDescription” line Error:”GoogleAuthenticationServerDescription doesn't exist in current context”.

Note: I had imported Google.Api namespace but I didn't found

Authentication.OAuth2.DotNetOpenAuth in namespace.

var provider = new WebServerClient(GoogleAuthenticationServer.Description);   
provider.ClientIdentifier =”MyclientID”;
provider.ClientSecret = “MySecretID;
var auth = new OAuth2Authenticator<WebServerClient>(provider, GetAuthorization); 
var service = new CoordinateService(new BaseClientService.Initializer());
Job jobBody = new Job();
jobBody.Kind = "Coordinate#job";
jobBody.State = new JobState();
jobBody.State.Kind = "coordinate#jobState";
jobBody.State.Assignee = "user@example.com";

//Create the Job
JobsResource.InsertRequest
ins1=service.Jobs.Insert(jobBody,"TeamID","Address",17.854425,75.51869,"Test");

================================================================================

Web request code for inserting job:In this scenario I am getting a error like 401(unauthorized). I am confused how to pass outh access token using web request.

Here is the code:

double latitude = Convert.ToDouble(tbLatitude.Text);
            double longitude = Convert.ToDouble(tbLogitude.Text);               
        String appURL = "https://www.googleapis.com/coordinate/v1/teams/TeamID/jobs/"; 
           string  strPostData = String.Format("teams={0},&job={1}&address={2}&lat=
 {3}&lng={4}&title={5}&key={6}",tbTeamID.Text, "?", tbAddress.Text, latitude,  
 longitude,  tbTitle.Text,"APIKEY");

 HttpWebRequest wrWebRequest = WebRequest.Create(appURL) as HttpWebRequest;

 wrWebRequest.Method = "POST";
 UTF8Encoding encoding = new UTF8Encoding();
 byte[] byteData = encoding.GetBytes(strPostData);
 wrWebRequest.ContentLength = strPostData.Length;
 wrWebRequest.ContentType = "application/json";
 wrWebRequest.UserAgent = "MyApplication/1.0";
 wrWebRequest.Referer = "https://www.googleapis.com/coordinate/v1/teams/teamId/jobs";

            // Post to the registration form.   

            StreamWriter objswRequestWriter = new 
 StreamWriter(wrWebRequest.GetRequestStream());

            objswRequestWriter.Write(strPostData);
            objswRequestWriter.Close();   

            // Get the response.      
            HttpWebResponse hwrWebResponse = 
 (HttpWebResponse)wrWebRequest.GetResponse();

            StreamReader objsrResponseReader = new  
 StreamReader(hwrWebResponse.GetResponseStream());

            string strResponse = objsrResponseReader.ReadToEnd();
Cœur
  • 37,241
  • 25
  • 195
  • 267
Sham Yemul
  • 463
  • 7
  • 30

1 Answers1

0

Why don't you use the .NET client library for Google Maps Coordinate? https://developers.google.com/api-client-library/dotnet/apis/coordinate/v1?

It looks like you are not using the OAuth 2.0 flow. You can read more about it in the client library's documentation - https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth.

You should also take a look in one of our samples, for example take a look in the Books sample , and specifically in the OAuth 2.0 code. Your code should look something like:

 UserCredential credential;
 using (var stream = new FileStream("client_secrets.json",
                                    FileMode.Open, FileAccess.Read))
 {
     credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
         GoogleClientSecrets.Load(stream).Secrets,
         new[] { [CoordinateService.Scope.Coordinate },
         "user", CancellationToken.None,
         new FileDataStore("CredentialsStore"));
 }

 // Create the service.
 var service = new CoordinateService(new BaseClientService.Initializer()
 {
     HttpClientInitializer = credential,
     ApplicationName = "Coordinate .NET library",
 });

 ... and here call one of the service's methods.

UPDATE (Oct 9th):

I see that you updated the code above, but why don't you use the latest API - http://www.nuget.org/packages/Google.Apis.Coordinate.v1/ with the latest OAuth 2.0 documentation, which I already mentioned - https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth?

I think you use the http://www.nuget.org/packages/Google.Apis.Authentication/1.6.0-beta which is deprected!

peleyal
  • 3,472
  • 1
  • 14
  • 25
  • I will check google .net client library and update. I did not mentioned in my original comment, the reason to use web request was, if successful, I can implement in MS Access VBA code. – Sham Yemul Oct 06 '14 at 07:02
  • And can't you do that with the .NET client library? – peleyal Oct 06 '14 at 15:38
  • when using .Net client library sample code, I am getting (;) expected error while assinging credential variable – Sham Yemul Oct 07 '14 at 05:24
  • I got compile error on below lines: 1) private async Task Run() Error:async could not be found and on Run()I get semicolon(;)excepted error. 2)credential=await GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,new[]{ CoordinateService.Scope.Coordinate},"user",CancellationToken.None, new FileDataStore("Coordinate.Auth.Store")); Error:await doesn't exist in current context and GoogleWebAuthorizationBroker(all parameter)I get semicolon(;)excepted error. I am running this sample on Visual Studio 2010 Note:I want to use google coordinate service. – Sham Yemul Oct 08 '14 at 06:46
  • Can you please update your code and add the stacktrace, it will make it much clearer – peleyal Oct 08 '14 at 12:59