2014-10-02 40 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

相關問題