Как использовать клиентскую библиотеку Google.Apis.CloudScheduler.v1beta1 для создания рабочих мест в .NET? - PullRequest
0 голосов
/ 06 февраля 2019

Я пытаюсь создать вакансии в облачном графике Google из моего веб-интерфейса.

Я могу создать работу с помощью клиентской библиотеки, но я считаю, что она не публикуется в Google.

    CloudSchedulerService cloudScheduler = new CloudSchedulerService();
    IDictionary<string, string> header = new Dictionary<string, string>();
    header.Add("Content-Type", "application/json");
    header.Add("Authorization", "Bearer sk_test_XKokBfNWv6FIYuTMg5sLPjhJ");
    HttpTarget httpTarget = new HttpTarget()
    {
        Body = "Check",
        Headers = header,
        HttpMethod = "POST",
        Uri = "******"
    };
    Job job = new Job()
    {
        Description = "testing",
        HttpTarget = httpTarget,
        Name = "projects/******/locations/europe-west3/jobs/testjob4",
        Schedule = "5 * * * *",
        TimeZone = "Asia/Kuwait"
    };
    cloudScheduler.Projects.Locations.Jobs.Create(job, "projects/******/locations/europe-west3");

1 Ответ

0 голосов
/ 07 февраля 2019

Я решил это, используя googleCredential с учетной записью службы, созданной из консоли Google.

  public static string[] scope =
    {
        CloudSchedulerService.Scope.CloudPlatform
    };
public static void CreateJob()
    {
 GoogleCredential googleCredential = GoogleCredential.FromFile("filepath").CreateScoped(scope);

            if (googleCredential != null)
            {
                ICredential credential = googleCredential.UnderlyingCredential;
                ServiceAccountCredential serviceAccountCredential = credential as ServiceAccountCredential;

                CloudSchedulerService cloudScheduler = new CloudSchedulerService(new Google.Apis.Services.BaseClientService.Initializer()
                {
                    HttpClientInitializer = googleCredential,
                    GZipEnabled = false
                });
                IDictionary<string, string> header = new Dictionary<string, string>();
                header.Add("Content-Type", "application/json");
                header.Add("Authorization", "Bearer "+jobDetails.httpTarget.Authorization);
                HttpTarget httpTarget = new HttpTarget()
                {
                    Body = Base64Encode("json string"),
                    Headers = header,
                    HttpMethod = "POST",
                    Uri = "*********"
                };
                Job job = new Job()
                {
                    Description = "",
                    HttpTarget = httpTarget,
                    Name = "projects/" + serviceAccountCredential.ProjectId + "/locations/europe-west3/jobs/" + jobId,
                    Schedule = "5 * * * *", //cron formate
                    TimeZone = "Asia/Kuwait"
                };
                cloudScheduler.Projects.Locations.Jobs.Create(job, "projects/" + serviceAccountCredential.ProjectId + "/locations/europe-west3").Execute();

}}

...