【问题标题】:google oauth 2 authorization when using their indexing apigoogle oauth 2 使用他们的索引 api 时的授权
【发布时间】:2018-07-09 20:56:56
【问题描述】:

我试图理解google indexing api,但他们的文档很糟糕。我已经完成了服务帐户的设置并下载了 json 文件以及剩余的先决条件。下一步是获取访问令牌以进行身份​​验证。

我在 .net 环境中,但他们没有为此提供示例。我确实找到了一些使用 .net 库来执行此操作的示例here,但是在以下代码之后,我不确定将创建什么服务来调用索引 api。我在 nuget 包管理器中没有看到 google.apis.indexing 库。

UserCredential credential;
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
{
    credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
        GoogleClientSecrets.Load(stream).Secrets,
        new[] { "https://www.googleapis.com/auth/indexing" },
        "user", CancellationToken.None, new FileDataStore("IndexingStore"));
}

在他们的example code 中,它看起来只是一个简单的 json 帖子。我试过了,但当然它不起作用,因为我没有经过身份验证。我只是不确定如何在 .net 环境中将所有这些联系在一起。

【问题讨论】:

    标签: google-oauth google-indexing-api


    【解决方案1】:

    您是对的,Google 的相关文档要么不存在,要么很糟糕。甚至他们自己的文档中也有损坏或未完成的页面,在其中一个文档中,您会被指向一个不存在的 nuget 包。可以通过将 SA 上的其他 Auth 示例组合在一起然后遵循 Java 索引文档来实现此功能。

    首先,您需要使用 nuget 包管理器添加主 api 包和 auth 包:

    然后尝试以下操作:

    using System;
    using System.Configuration;
    using System.IO;
    using System.Net.Http;
    using System.Text;
    using System.Threading.Tasks;
    using Google.Apis.Auth.OAuth2;
    using Google.Apis.Http;
    using Newtonsoft.Json;
    
    namespace MyProject.Common.GoogleForJobs
    {
        public class GoogleJobsClient
        {
            public async Task<HttpResponseMessage> AddOrUpdateJob(string jobUrl)
            {
                return await PostJobToGoogle(jobUrl, "URL_UPDATED");
            }
    
            public async Task<HttpResponseMessage> CloseJob(string jobUrl)
            {
                return await PostJobToGoogle(jobUrl, "URL_DELETED");
            }
    
            private static GoogleCredential GetGoogleCredential()
            {
                var path = ConfigurationManager.AppSettings["GoogleForJobsJsonFile"];
                GoogleCredential credential;
                using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    credential = GoogleCredential.FromStream(stream)
                        .CreateScoped(new[] { "https://www.googleapis.com/auth/indexing" });
                }
    
                return credential;
            }
    
            private async Task<HttpResponseMessage> PostJobToGoogle(string jobUrl, string action)
            {
                var googleCredential = GetGoogleCredential();
                var serviceAccountCredential = (ServiceAccountCredential) googleCredential.UnderlyingCredential;
    
                const string googleApiUrl = "https://indexing.googleapis.com/v3/urlNotifications:publish";
    
                var requestBody = new
                {
                    url = jobUrl,
                    type = action
                };
    
                var httpClientHandler = new HttpClientHandler();
    
                var configurableMessageHandler = new ConfigurableMessageHandler(httpClientHandler);
    
                var configurableHttpClient = new ConfigurableHttpClient(configurableMessageHandler);
    
                serviceAccountCredential.Initialize(configurableHttpClient);
    
                HttpContent content = new StringContent(JsonConvert.SerializeObject(requestBody), Encoding.UTF8, "application/json");
                var response = await configurableHttpClient.PostAsync(new Uri(googleApiUrl), content);
    
                return response;
            }
        }
    }
    

    你可以这样称呼它

    var googleJobsClient = new GoogleJobsClient();
    var result = await googleJobsClient.AddOrUpdateJob(url_of_vacancy);
    

    或者如果你不在异步方法中

    var googleJobsClient = new GoogleJobsClient();
    var result = googleJobsClient.AddOrUpdateJob(url_of_vacancy).Result;
    

    【讨论】:

    • 感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 2012-11-12
    相关资源
    最近更新 更多