(由于声誉而删除了http) 我正在使用" GoogleApisSamples"来测试Google Site Verification API。来自Google的项目,但我有关于重定向uri的问题..我从我的GoogleDrive应用程序获取了client_secrets.json(带有重定向uris设置),但是该程序获得的重定向uri就像" localhost:1168 /授权/" (它改变)。我将重定向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!");
}
}
}
我的&#34; client_secrets.json&#34; (我改变了帽子里的东西)
{
"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"
]
}
}
And the error I get is:
错误:redirect_uri_mismatch
应用程序:GoogleApisSamples
请求中的重定向URI:localhost:1168 / authorize /与注册的重定向URI不匹配。
答案 0 :(得分:2)
重定向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",
});
答案 1 :(得分:0)
感谢您的回答。这是我认为唯一明确提到的地方,对于Web应用程序类型,redirect_uri必须映射到实际文件。我使用ASP.Net MVC应用程序,并将动作作为redirect_uri(错误)。当我将其更改为实际的* .cshtml文件时,一切正常!