我们正在使用Google Analytics API v3 (点网版)在我们的网站上报告一些统计数据.我的代码在我的本地计算机上正常运行,但由于某些防火墙规则,它无法在生产服务器上运行.我们的系统管理员建议尝试使用代理.我在互联网上搜索了为Google AnalyticsAPI服务设置代理的任何指南,但没有运气.感谢这方面的任何指示.
编辑:
public DataTable GetSearchTrends()
{
string GoogleAnalyticsProfileId = AppConfigManager.GetGoogleAnalyticsProfileIdForInis();
var service = new AnalyticsService(new BaseClientService.Initializer()
{
Authenticator = Authenticate()
});
DataResource.GaResource.GetRequest request = service.Data.Ga.Get(
GoogleAnalyticsProfileId,
string.Format("{0:yyyy-MM-dd}", StartDate),
string.Format("{0:yyyy-MM-dd}", EndDate),
GoogleAnalyticsSearchUniquesMetric
);
request.Dimensions = GoogleAnalyticsSearchKeywordMetric;
request.Sort = string.Concat("-", GoogleAnalyticsSearchUniquesMetric);
request.MaxResults = NumberOfSearchTrendsToFetch;
GaData response = request.Fetch();
return SearchTrendsHelper.ConvertToDataTable(
response.Rows,
SearchTrendsKeywordsExcludeList,
NumberOfSearchTrendsToDisplay
);
}
private IAuthenticator Authenticate()
{
string GoogleAnalyticsServiceScope = AnalyticsService.Scopes.AnalyticsReadonly.GetStringValue();
string GoogleApiServiceAccountId = AppConfigManager.GetGoogleApiServiceAccountId();
string GoogleApiServiceAccountKeyFile = AppConfigManager.GetGoogleApiServiceAccountKeyFile();
string GoogleApiServiceAccountKeyPassword = AppConfigManager.GetGoogleApiServiceAccountKeyPassword();
AuthorizationServerDescription …Run Code Online (Sandbox Code Playgroud) .net google-api google-analytics-api google-api-dotnet-client
我在尝试运行我的c#控制台应用程序时收到此错误...我正在尝试调用google calender api v3来获取日历并将事件添加到日历中.根据google-api-dotnet-client的示例代码,我这样做.(https://code.google.com/p/google-api-dotnet-client/source/browse/Calendar.VB.ConsoleApp/Program .vb?repo = samples)这是vb.net代码.我将它转换为c#代码后使用此示例.
这是我的代码:
class Program
{
static void Main(string[] args)
{
try
{
new Program().Run().Wait();
}
catch (AggregateException ex)
{
foreach (var e in ex.InnerExceptions)
{
Console.WriteLine("ERROR: " + e.Message);
}
}
}
private async Task Run()
{
UserCredential credential;
IList<string> scopes = new List<string>();
CalendarService service;
scopes.Add(CalendarService.Scope.Calendar);
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
{
// problem occuring during executing this statement.
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
scopes,
"user", CancellationToken.None, new …Run Code Online (Sandbox Code Playgroud) 我正在尝试设置c#代码来管理我们的Google域名.
每当我调用service.Users.List()或DirectoryService api中的任何其他方法时,我都会收到此错误.
Google.Apis.Requests.RequestError
Insufficient Permission [403]
Errors [
Message[Insufficient Permission] Location[ - ] Reason[insufficientPermissions] Domain[global]
]
Run Code Online (Sandbox Code Playgroud)
我按照OAuth设置上的所有说明进行操作.我使用的帐户是域管理员.
当我使用GAM.exe执行相同的操作时,我使用的客户端密码文件工作正常.这让我相信我在代码中做错了.
下面是我查询用户的代码,有什么我遗漏的吗?
static void Main(string[] args)
{
var applicationName = "App Project Name";
var userName = "admin@domain.com";
var clientID = "clientIDfromAPIcredentialpageonconsole.developers.google.com";
UserCredential credential;
using (var stream = new FileStream("C:\\gam\\client_secrets.json", FileMode.Open, FileAccess.Read))
{
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { DirectoryService.Scope.AdminDirectoryOrgunit, DirectoryService.Scope.AdminDirectoryUser },
userName,
CancellationToken.None, null).Result;
}
var service = new DirectoryService(new BaseClientService.Initializer()
{
ApplicationName = applicationName,
HttpClientInitializer = credential
});
var list = …Run Code Online (Sandbox Code Playgroud) 尝试使用具有Google .net客户端库的服务帐户将Web应用程序部署到Azure时,它返回时出现以下错误
502 - Web服务器在充当网关或代理服务器时收到无效响应.
代码示例:
var certificate = new X509Certificate2(KeyFilePath, "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail) {
Scopes = new string[] { AnalyticsService.Scope.Analytics };
}.FromCertificate(certificate));
// Create the service.
AnalyticsService service = new AnalyticsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Analytics API Sample",
});
Run Code Online (Sandbox Code Playgroud)
上面的代码在开发中运行时有效.但是,当部署到AZURE时,它会返回错误.
Google 最近开始为我们提供服务帐户的 Json 密钥文件,而不是 P12 密钥文件。我一直在努力解决这个问题,那里没有很多信息,我所看到的信息表明这应该有效。
string[] scopes = new string[] { DriveService.Scope.Drive};
Stream stream = new FileStream(jsonKeyFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
var credential = GoogleCredential.FromStream(stream).CreateScoped(scopes);
Run Code Online (Sandbox Code Playgroud)
但是它抛出以下异常
从 JSON 创建凭据时出错。无法识别的凭据类型。
我已经仔细检查了 json 密钥文件,下载了两个不同的文件,试图让它不起作用。
c# google-api google-oauth google-api-dotnet-client service-accounts
我一直在互联网上搜索几个小时,试图找出上传一个将包含在Team Drive中的文件的必要条件.
我已经阅读了大部分文档,我发现的团队驱动器中唯一有趣的部分/提及是在这里,但遗憾的是没有具体信息:https:
//developers.google.com/drive/v3/web/manage-uploads
https: //developers.google.com/drive/v3/web/manage-teamdrives
https://developers.google.com/drive/v3/web/about-files
我正在使用.Net gapi nuget包(v3).创建如下服务:
string[] scopes = new string[] { DriveService.Scope.Drive, DriveService.Scope.DriveFile };
var secrets = new ClientSecrets
{
ClientId = "...",
ClientSecret = "...",
};
var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(secrets, scopes, Environment.UserName, CancellationToken.None).Result;
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "...",
});
Run Code Online (Sandbox Code Playgroud)
我有我目标的Team Drive的ID,我可以通过以下代码成功检索TeamDrive,但这里没有有趣的方法可供上传:
var teamDrive = service.Teamdrives.Get(driveFolderId).Execute();
Run Code Online (Sandbox Code Playgroud)
我目前一直在尝试使用正常的CreateMediaUpload方式创建文件.
File body = new File();
body.Name = name;
body.MimeType = "application/octet-stream";
FilesResource.CreateMediaUpload request = service.Files.Create(body, stream, "text/plain"); …Run Code Online (Sandbox Code Playgroud) 我有一个代码,其目的是重命名特定工作表,但是当执行 BatchUpdate 并且代码被破坏时,有人有任何想法吗?
public void UpdateSheetName(string sheetName,string newSheetName)
{
//get sheet id by sheet name
Spreadsheet spr = service.Spreadsheets.Get(SpreadsheetId).Execute();
Sheet sh = spr.Sheets.Where(s => s.Properties.Title == sheetName).FirstOrDefault();
int sheetId = (int)sh.Properties.SheetId;
BatchUpdateSpreadsheetRequest bussr = new BatchUpdateSpreadsheetRequest();
var request = new Request()
{
UpdateSpreadsheetProperties= new UpdateSpreadsheetPropertiesRequest(){
Properties=new SpreadsheetProperties()
{
Title= newSheetName,
},
Fields ="title"
}
};
bussr.Requests = new List<Request>();
bussr.Requests.Add(request);
var bur = service.Spreadsheets.BatchUpdate(bussr, SpreadsheetId);
bur.Execute();
}
Run Code Online (Sandbox Code Playgroud)
错误消息:“requests[0]”(oneof)的值无效,oneof 字段“kind”已设置。无法设置“updateSpreadsheetProperties”[400]
我正在使用 .net 包Google.Apis.Sheets.v4 Client Library。一切都很好,但上次(大约一周前)出现错误:The request is missing a valid API key. [403]当我试图batchGet在查询中使用 150 多个范围时。如果我在单个查询中将我的查询拆分为 25 个范围,这可能没问题,也可能在其中一个返回相同的错误。有时它可以在没有任何拆分的情况下工作,但通常我会收到错误消息。我绝对确定我的 API 密钥是有效的,我已经尝试了另一个密钥、另一个电子表格,但没有任何改变。
我正在使用从 Nuget 下载的 Youtube .Net 库。
我创建了一个 Windows 服务,它正在检查一个文件夹以将新视频上传到 youtube。我使用控制台应用程序进行了测试,对于这种情况,用户必须在网络浏览器上手动进行授权,之后我可以毫无问题地将视频上传到 youtube。问题是当我尝试使用离线访问时。
当我在 Windows 服务上运行我的代码时,我无法从用户那里获得手动授权,所以我按照这个答案创建了我的访问令牌:https : //stackoverflow.com/a/20689423/2277059
问题是我没有找到任何使用独立版本的代码示例,所以我有点迷茫。使用我当前的代码,我在尝试上传视频时总是遇到错误:“unauthorized_client”。您可以在我的代码“client_secrets.json”中看到的 JSON 文件是您在为 YouTube API V3 创建凭据时自动生成的文件。
我也试过这个:https : //stackoverflow.com/a/43895931/2277059,但得到了同样的错误。
现在我没有从代码中刷新令牌,只是在 OAuth Playground 上进行并测试服务。一旦成功,我将研究如何在每次查询时刷新令牌。
创建凭据时,我选择了“其他”类型,因为这是 Windows 服务。
是我的代码有问题还是我在配置中遗漏了什么?
这是我的代码:
var token = new TokenResponse()
{
AccessToken = "werwdsgfdg...",
ExpiresInSeconds = 3600,
RefreshToken = "3/dsdfsf...",
TokenType = "Bearer"
};
Log.Info("Generating user credentials and secrets");
UserCredential credential;
string credentialsPath = System.AppDomain.CurrentDomain.BaseDirectory + "client_secrets.json";
using (var stream = new FileStream(credentialsPath, FileMode.Open, FileAccess.Read))
{
credential …Run Code Online (Sandbox Code Playgroud) c# google-api google-oauth google-api-dotnet-client youtube-data-api
我正在尝试使用 C# 连接到新的 Google Analytics Data api,以从新的 Google Analytics GA4 请求数据。我能找到的唯一示例是 Quickstart 客户端库 .net这确实有效,但它使用服务帐户。云 .net 客户端库google-cloud-dotnet仅包含使用服务帐户的示例。
当我尝试传递桌面应用程序凭据以使用 Oauth”授权时,我得到
从 JSON 创建凭据时出错。无法识别的凭据类型。
using System;
using System.Threading;
using System.Threading.Tasks;
using Google.Analytics.Data.V1Beta;
namespace GoogleAnalyticsExamplesData
{
class Program
{
private const string PropertyId = "250796939";
private const string PathToCreds = @"C:\dev\ServiceAccountCred.json";
static async Task Main(string[] args)
{
Console.WriteLine("Hello World!");
// Check whether the environment variable exists.
var environmentVariable = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");
// If necessary, create it.
if (environmentVariable == null)
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", PathToCreds); …Run Code Online (Sandbox Code Playgroud) c# google-api google-api-dotnet-client google-analytics-4 google-analytics-data-api