无法使用Google协调服务创建作业

时间:2014-10-02 12:13:00

标签: c# google-maps google-api google-api-dotnet-client

Visual Studio 2010 框架4.0

我正在尝试使用Google坐标API插入作业。对于插入作业我尝试了两种方法。使用webrequest并协调API。但是我无法插入作业。 使用Google Coordinate API:获取错误"身份验证并不存在于命名空间google.api"

我使用nuget安装了谷歌坐标API。我使用下面的代码使用坐标API插入作业。但是我在“GoogleAuthenticationServerDescription”行上收到错误 错误:“GoogleAuthenticationServerDescription在当前上下文中不存在”。

注意:我导入了Google.Api命名空间,但我没有找到

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";

//创建作业
    JobsResource.InsertRequest
    INS1 = service.Jobs.Insert(jobBody,&#34; TeamID&#34;&#34;地址&#34;,17.854425,75.51869,&#34;试验&#34);

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

用于插入作业的Web请求代码:在这种情况下,我收到类似401(未经授权)的错误。我很困惑如何使用Web请求传递outh访问令牌。

以下是代码:

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();

1 个答案:

答案 0 :(得分:0)

为什么不将.NET客户端库用于Google Maps Coordinate? https://developers.google.com/api-client-library/dotnet/apis/coordinate/v1

您好像没有使用OAuth 2.0流程。您可以在客户端库的文档中了解更多相关信息 - https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth

您还应该查看我们的一个示例,例如,查看Books sample ,特别是OAuth 2.0代码。您的代码应该类似于:

 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.

更新(10月9日):

我看到您更新了上述代码,但为什么不使用最新的API http://www.nuget.org/packages/Google.Apis.Coordinate.v1/和最新的OAuth 2.0文档,我已经提到过https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth

我认为你使用了被删除的http://www.nuget.org/packages/Google.Apis.Authentication/1.6.0-beta