2014-10-02 42 views
0

Visual Studio 2010中 框架4.0无法创建使用谷歌协调工作服务

我尝试使用谷歌的API。有关协调工作插入我试图插入一个作业二都ways.Using的WebRequest和协调API.But我我无法插入这份工作。 使用谷歌API坐标:我安装谷歌协调使用nuget.I API下面的代码用于使用坐标API.But我上“收到错误插入的工作得到错误“验证在名字空间不存在google.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 = "[email protected]"; 

//创建工作
JobsResource.InsertRequest
INS1 = service.Jobs.Insert(jobBody,“TeamID ”, “地址”,17.854425,75.51869, “测试”);

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

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

下面是代码:

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

回答

0

你为什么不使用谷歌地图.NET客户端库坐标? 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. 

UPDATE(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,这是废除!

+0

我会检查谷歌.net客户端库和更新。我在原来的评论中没有提到,使用Web请求的原因是,如果成功的话,我可以在MS Access VBA代码中实现。 – 2014-10-06 07:02:00

+0

你不能用.NET客户端库来做这件事吗? – peleyal 2014-10-06 15:38:14

+0

使用.Net客户端库示例代码时,我得到(;)期望错误,同时确认凭据变量 – 2014-10-07 05:24:23

相关问题