2009-11-01 131 views
1

嘗試從Google Analytics API獲取饋送時出現此錯誤。不過,使用相同的標記,我會從Google日曆獲取成功的Feed。兩者之間的代碼除了提供網址之外完全相同。因此,它必須與Google Analytics(分析)爲https和Calendar(日曆)有關。HTTP/1.1 401令牌無效 - AuthSub令牌有錯誤的範圍

我已經成功創建了一個不安全的長期令牌。同時要求初始令牌範圍參數:

scope=http%3A%2F%2Fwww.google.com%2Fcalendar%2Ffeeds%2F%20https%3A%2F%2Fwww.google.com%2Fanalytics%2Ffeeds 

我對長期令牌請求:

GET /accounts/AuthSubSessionToken HTTP/1.1 
Authorization: AuthSub token="CP_AgsyLDxDCtpjg-f____8B" 
Content-Type: application/x-www-form-urlencoded 
Host: www.google.com:443 
Accept: text/html, */* 
Accept-Encoding: identity 
User-Agent: Mozilla/3.0 (compatible; Indy Library) 

返回一個長期令牌。使用它的谷歌日曆:

GET /calendar/feeds/default/allcalendars/full HTTP/1.1 
Connection: keep-alive 
Content-Type: application/x-www-form-urlencoded 
Authorization: AuthSub token="CP_AgsyLDxCh2tmj-P____8B" 
Host: www.google.com 
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2 
Accept-Encoding: identity 
User-Agent: Mozilla/3.0 (compatible; Indy Library) 

返回臨時重定向(302):

HTTP/1.1 302 Moved Temporarily 
Expires: Sun, 01 Nov 2009 03:00:01 GMT 
Date: Sun, 01 Nov 2009 03:00:01 GMT 
Set-Cookie: S=calendar=mta4-_BxxANrylcSnzUatg;Expires=Mon, 01-Nov-2010 03:00:01 GMT 
Location: http://www.google.com/calendar/feeds/default/allcalendars/full?gsessionid=mta4-_BxxANrylcSnzUatg 

導致一個成功的得到這個:

GET /calendar/feeds/default/allcalendars/full?gsessionid=mta4-_BxxANrylcSnzUatg HTTP/1.1 
Connection: keep-alive 
Content-Type: application/x-www-form-urlencoded 
Authorization: AuthSub token="CP_AgsyLDxCh2tmj-P____8B" 
Host: www.google.com 
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2 
Accept-Encoding: identity 
User-Agent: Mozilla/3.0 (compatible; Indy Library) 
Cookie: S=calendar=mta4-_BxxANrylcSnzUatg 

但我得到的錯誤401試圖獲取Google Analytics Feed時:

GET /analytics/feeds/accounts/default HTTP/1.1 
Content-Type: application/x-www-form-urlencoded 
Authorization: AuthSub token="CP_AgsyLDxCh2tmj-P____8B" 
Host: www.google.com:443 
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2 
Accept-Encoding: identity 
User-Agent: Mozilla/3.0 (compatible; Indy Library) 

我的域名需要有效的SSL證書嗎?一直在爭取這個星期!
在Apache中使用Indy10和Delphi 2007。

請求提供一些Delphi代碼。我在這裏提供的是用於GET的代碼。我不提供代碼來獲取令牌,因爲我認爲它們很好(我可以獲取日曆提要)。

var 
    IdHTTP: TIdHTTP; 
    IdSSLIOHandlerSocket1: TIdSSLIOHandlerSocketOpenSSL; 

begin 
    IdSSLIOHandlerSocket1 := TIdSSLIOHandlerSocketOpenSSL.create(nil); 
    IdHTTP := TIdHTTP.create(nil); 
    with IdSSLIOHandlerSocket1 do begin 
    SSLOptions.Method := sslvSSLv3; 
    SSLOptions.Mode := sslmUnassigned; 
    SSLOptions.VerifyMode := []; 
    SSLOptions.VerifyDepth := 2; 
    end; 
    with IdHTTP do begin 
    IOHandler := IdSSLIOHandlerSocket1; 
    ProxyParams.BasicAuthentication := False; 
    Request.UserAgent := 'Mozilla/3.0 (compatible; Indy Library)'; 
    Request.ContentType := 'application/x-www-form-urlencoded'; 
    request.host := 'www.google.com/analytics'; 
    request.connection := 'keep-alive'; 
    Request.Accept := 'text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2'; 
    end; 

    idhttp.Request.CustomHeaders.Add('Authorization: AuthSub token="'+mToken+'" '); 

    IdHTTP.Get('https://www.google.com/analytics/feeds/accounts/default'); // results in the 401 
+0

show是你的一些Delphi代碼,尤其是你如何做HTTPS處理 – 2009-11-01 08:08:11

+0

請將用戶代理設置爲代表* your *程序的字符串。使用通用值可以讓您的程序與其他沒有專門研究的人混在一起,這可能會導致服務器過濾您的程序,認爲這只是另一個DoS攻擊。 – 2009-11-01 20:36:23

+0

我嘗試將用戶代理設置爲獨特的: 用戶代理:測試Google Analytics界面 仍然是同樣的問題。 – 2009-11-02 01:18:33

回答

2

花了一些時間,並要求我使用Fiddler和Curl php方法。發現這一行:

Host: www.google.com:443 

不能有:443。一定是這樣的:

Host: www.google.com 

印地TIdHTTP組件已自動追加到該主機,當它看到HTTPS。我能解決這個問題的唯一方法就是發佈我自己的「主機」屬性。

+1

另一個解決方法是在標記範圍中包含:443;它只需要匹配。 – Jonathan 2009-12-15 08:27:15

+0

謝謝 - 我會試試看。 – 2009-12-15 15:41:26