.net - 未采用 Google Site Verification API .NET 重定向 uri

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

(http 因信誉问题被删除) 我正在测试 Google Site Verification API使用来自 Google 的“GoogleApisSamples”项目,但我有一个关于重定向 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”(我改变了大写的东西)

{
"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. That’s an error.

Error: redirect_uri_mismatch

Application: GoogleApisSamples

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

最佳答案

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

对于 native 应用程序的客户端 ID,您可以将其设置为以下内容:

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

对于 Web 应用程序的客户端 ID,它更像这样

    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",
            });

关于.net - 未采用 Google Site Verification API .NET 重定向 uri,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26174091/

相关文章:

c# - 在 LINQ to SQL 中搜索两列?

java - 软件框架和软件平台有什么区别?

json - 将带有子项的实体类型发布到(MVC Web Api)OData 服务

go - 服务器端 oauth : What to do with the tokens received

PHP : Google plus Oauth 2. 0 - 获取 OAuth2 访问 token 时出错,消息: 'invalid_client'

ruby-on-rails - 无法通过 GoogleOauth2 对您进行身份验证,因为 "Invalid credentials"

c# - 字典的多键查找

ruby-on-rails - 在 Rails 中,如何使用 View 呈现 JSON?

javascript - jQuery $.getJSON 对每个控件只工作一次。不再到达服务器

c# - 无法在 VS 2003 中打开旧的 ASP.NET 1.1 应用程序(解决方法)