【问题标题】:Google Site Verification API .NET redirect uri is not adopted未采用 Google Site Verification API .NET 重定向 uri
【发布时间】:2014-11-28 05:24:32
【问题描述】:

(http 因声誉而被删除) 我正在使用 Google 的“GoogleApisSamples”项目测试Google Site Verification API,但我遇到了关于重定向 uri 的问题。我从我的 GoogleDrive 应用程序中获取了 client_secrets.json(设置了重定向 uris),但是重定向 uri这个程序得到的类似于“localhost:1168/authorize/”(它会改变)。我将重定向 uri 设置为“www.google.com”和“www.google.com/”。

namespace SiteVerification.VerifySite

{

internal class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        // Display the header and initialize the sample.
        Console.WriteLine("Site Verification sample");
        Console.WriteLine("========================");

        UserCredential credential;
        using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
        {
            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                new[] { SiteVerificationService.Scope.Siteverification },
                "user", CancellationToken.None, new FileDataStore("SiteVerification.VerifySite")).Result;
        }

        // Create the service.
        var service = new SiteVerificationService(new BaseClientService.Initializer
            {
                HttpClientInitializer = credential,
                ApplicationName = "SiteVerification API Sample",
            });
        RunVerification(service);

        Console.WriteLine("Press any key to continue...");
        Console.ReadKey();
    }

    /// <summary>
    /// This method contains the actual sample code.
    /// </summary>
    private static void RunVerification(SiteVerificationService service)
    {
        // Request user input.
        Console.WriteLine("Please enter the URL of the site to verify:");
        var site = Console.ReadLine();
        Console.WriteLine();

        // Example of a GetToken call.
        Console.WriteLine("Retrieving a meta token ...");
        var request = service.WebResource.GetToken(new SiteVerificationWebResourceGettokenRequest()
        {
            VerificationMethod = "meta",
            Site = new SiteVerificationWebResourceGettokenRequest.SiteData()
            {
                Identifier = site,
                Type = "site"
            }
        });
        var response = request.Execute();
        Console.WriteLine("Token: " + response.Token);
        Console.WriteLine();

        Console.WriteLine("Please place this token on your webpage now.");
        Console.WriteLine("Press ENTER to continue");
        Console.ReadLine();
        Console.WriteLine();

        // Example of an Insert call.
        Console.WriteLine("Verifying...");
        var body = new SiteVerificationWebResourceResource();
        body.Site = new SiteVerificationWebResourceResource.SiteData();
        body.Site.Identifier = site;
        body.Site.Type = "site";
        var verificationResponse = service.WebResource.Insert(body, "meta").Execute();

        Console.WriteLine("Verification:" + verificationResponse.Id);
        Console.WriteLine("Verification successful!");
    }
}

}

还有我的“client_secrets.json”(我把 Stuff 改成了大写)

{
"web": {
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "client_secret": "CLIENT_SECRET",
    "token_uri": "https://accounts.google.com/o/oauth2/token",
    "client_email": "STUFF",
    "redirect_uris": [
        "http://www.google.com/",
        "http://www.google.com"
    ],
    "client_x509_cert_url": "STUFF",
    "client_id": "CLIENT_ID",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "javascript_origins": [
        "https://www.google.com"
    ]
}

}

我得到的错误是:

  1. 这是一个错误。

错误:redirect_uri_mismatch

应用:GoogleApisSamples

请求中的重定向 URI:localhost:1168/authorize/ 与注册的重定向 URI 不匹配。

【问题讨论】:

  • 此代码用于站点服务 API,为什么您将其标记为 Google Drive api?
  • 对不起,我是这个话题的新手。而且我找不到 SiteServiceAPI 的标签(由于我的代表,我也无法创建一个)
  • 您是否将 client_secrets.json 替换为您从开发控制台中的应用程序中获得的 client_secrets.json,还是仍在使用示例代码附带的那个?
  • 我用我的 client_secrets.json 替换了它(json 看起来像上面显示的那个)
  • 如果我在 URL 中手动将 redirect_uri 更改为“google.com”,我会被要求获得许可,然后重定向到 google.com

标签: .net json google-oauth google-sites google-site-verification-api


【解决方案1】:

重定向 URI 必须与您希望将身份验证返回到的位置匹配

对于Client ID for native application,您可以将其设置为以下内容:

 Redirect URIs     urn:ietf:wg:oauth:2.0:oob  
                   http://localhost

对于Client ID for web application,它会更像这样

    Redirect URIs     
         http://localhost/google-api-php-client-samples/oauth2.php 

Web 必须修补到实际文件。

这个例子可能更容易使用将文件加载到流中。

string[] scopes = new string[] { SiteVerificationService.Scope.Siteverification };
// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets
                                           {
                                            ClientId = CLIENT_ID,
                                            ClientSecret = CLIENT_SECRET
                                             },
                   scopes,
                   Environment.UserName,
                   CancellationToken.None,
                   new FileDataStore"Daimto.SiteVerification.Auth.Store")).Result;

// Create the service.
var service = new SiteVerificationService(new BaseClientService.Initializer
            {
                HttpClientInitializer = credential,
                ApplicationName = "SiteVerification API Sample",
            });

【讨论】:

  • 如你所说,我已经创建了自己的应用程序。现在我必须在我的程序中更改我的 ApplicationName 和“用户”吗?我没有更改json文件中的redirect_uris。
  • 在开发者控制台的应用程序中设置了重定向 uri。您不要在文件中更改它。
  • 是的,我没有在文件中更改它们。我确实编辑了“Web 应用程序的客户端 ID”设置并将重定​​向 URIS 更改为“google.com”和“google.com”。
  • 您有一个文件可以处理 Google.com 上的身份验证吗?重定向 url 需要指向你的服务器
  • 哦,这就是你的意思,好吧,我必须改变它。我现在使用 URL“example.com/oauth2callback”,但“localhost:4115”的问题仍然存在。
【解决方案2】:

感谢您的回答。这是我认为明确提到的唯一地方,对于 Web 应用程序类型,redirect_uri 必须映射到实际文件。我使用 ASP.Net MVC 应用程序并将操作作为 redirect_uri (错误)。当我将其更改为实际的 *.cshtml 文件时,一切正常!!!

【讨论】:

  • 但是你是如何设置redirect_uri的?我在哪里看不到
  • 抱歉回复晚了。转到开发人员控制台-> 凭据-> OAuth2 凭据->(选择类型为 Web 应用程序)。在该屏幕中,您将看到重定向 uri,
猜你喜欢
  • 2018-01-29
  • 2017-02-13
  • 2014-08-08
  • 2014-10-13
  • 2019-07-26
  • 2012-09-27
  • 2014-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多