Visual Studio 2010框架 4.0
我正在尝试使用谷歌坐标 API 插入作业。对于插入工作,我尝试了两种方法。使用 webrequest 和坐标 API。但我无法插入工作。使用 Google 坐标 API:获取错误"命名空间 google.api 中不存在身份验证">
我使用 nuget 安装了谷歌坐标 API。我使用以下代码使用坐标 API 插入作业。但是我在"谷歌身份验证服务器描述"行上收到错误错误:"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=服务。Jobs.Insert(jobBody,"TeamID","Address",17.854425,75.51869,"Test"(;
==
用于插入作业的 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();
为什么不使用 .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 中阅读有关它的更多信息。
您还应该查看我们的一个示例,例如查看 书籍示例 ,特别是 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!