2014-10-03 69 views
3

(因爲聲譽而被刪除了http) 我使用Google的「GoogleApisSamples」項目測試了Google Site Verification API,但是我遇到了有關重定向uri的問題..我得到我的GoogleDrive應用程序中的client_secrets.json(帶有重定向uris設置),但是此程序獲取的重定向URI類似於「localhost:1168/authorize /」(它發生更改)。我將重定向URI設置爲「www.google.com」和「www.google.com/」。Google Site Verification API沒有采用.NET重定向uri

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" 
    ] 
} 

}

And the error I get is: 
  1. 這是一個錯誤。

錯誤:redirect_uri_mismatch

應用:GoogleApisSamples

在請求重定向URI:本地主機:1168 /授權/不匹配的註冊重定向URI。

+0

此代碼適用於網站服務API爲什麼您將其標記爲Google Drive api? – DaImTo 2014-10-03 06:35:31

+0

Sry我是這個主題的新手。我無法找到SiteServiceAPI的標籤(我也不能創建一個,因爲我的代表) – carlilord 2014-10-03 07:00:05

+0

您是否用您在開發控制檯中的應用程序獲得的client_secrets.json替換client_secrets.json,還是仍在使用來自與示例代碼? – DaImTo 2014-10-03 07:26:57

回答

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

如您所說,我已經創建了自己的應用程序。現在我必須在我的Programm中更改我的ApplicationName和「user」嗎?我沒有改變json文件中的redirect_uris。 – carlilord 2014-10-03 09:39:19

+0

重定向uris在開發人員控制檯上的應用程序中設置。你不要在文件中改變它。 – DaImTo 2014-10-03 09:57:39

+0

是的,我沒有改變它們在文件中。我編輯了「Web應用程序的客戶端ID」設置,並將Redirect URIS更改爲「http://www.google.com/」和「http://www.google.com」。 – carlilord 2014-10-03 10:10:27

0

感謝您的回答。這是我認爲它唯一清晰提及的一個地方,那就是對於Web應用程序類型,redirect_uri必須映射到實際的文件。我使用了一個ASP.Net MVC應用程序,並以redirect_uri(Wrong)的形式給出了相應的操作。當我將其更改爲實際的* .cshtml文件時,它一切正常!

+0

但你是如何設置redirect_uri的?我看不到它在哪裏 – 2017-03-22 16:11:46

+0

對不起,遲到的迴應。轉到開發者控制檯 - >憑證 - > OAuth2憑證 - >(選擇鍵入爲Web應用程序)。在那個屏幕上你會看到重定向的uri, – 2017-06-20 08:52:34