簡體   English   中英

接收Google雲端硬盤推送通知

[英]Receiving Google Drive Push Notifications

通過使用此處所述的方法,我已經能夠建立一個通道來接收來自Google雲端硬盤的推送通知 我收到通知,一切正常。 我的問題是,當我收到推送通知時,只會得到以下信息:

Content-Length: 0
Accept: */*
Accept-Encoding: gzip,deflate,br
Connection: Keep-alive
Host: www.domain.com
User-Agent: APIs-Google; (+https://developers.google.com/webmasters/APIs-Google.html)
X-Goog-Channel-Expiration: Thu, 29 Dec 2016 00:00:00 GMT
X-Goog-Channel-Id: 01ecb23c-e718-8674-6ab3-623931741334
X-Goog-Message-Number: 2745870
X-Goog-Resource-Id: hw75x654x56jYhRNkfU5CFEXXXhtlj8
X-Goog-Resource-State: change
X-Goog-Resource-Uri: https://www.googleapis.com/drive/v3/changes?includeRemoved=true&pageSize=100&pageToken=658&restrictToMyDrive=false&spaces=drive&alt=json

根據本文檔 ,存在一些包含請求正文的 “更改”通知消息。 不幸的是,我無法獲得請求正文。

處理推送通知的腳本具有以下邏輯:

$oldcontent = file_get_contents('notifications.txt');

$newnotsfile = fopen("notifications.txt", "w");

$post = file_get_contents('php://input');
$requestBody = json_decode($post , TRUE); //convert JSON into array

$time = date("Y-M-d H:i:s", time());
fwrite($newnotsfile , "<br><br>---------------- │ Time: ".$time."<br><br>");

foreach (getallheaders() as $name => $value) {
    fwrite($newnotsfile , $name.": ".$value."<br>");
} 

fwrite($newnotsfile , $requestBody );
fwrite($newnotsfile , "<br><br>");

fwrite($newnotsfile , $oldcontent);
fclose($newnotsfile );

?>

我認為通過使用$post = file_get_contents('php://input'); 我會捕獲請求正文,但事實是它什么也沒捕獲。 如果我理解正確,則應該收到具有此處詳細結構的變更資源。 我在做錯什么嗎?還是我了解這個錯? 感謝您提供的任何見解,並在此先感謝!

實際上,在Webhook通知中沒有發送請求的正文。 因此,一旦更改到達回調URL,將通過獲取對更改資源uri的get請求來獲取更改,如下所示

Resource URI : https://www.googleapis.com/drive/v3/changes?includeRemoved=true&pageSize=100&pageToken=895&restrictToMyDrive=false&spaces=drive&alt=json

或者可以使用以下代碼以編程方式獲取更改

String pageToken = channelInfo.getCurrPageToken();
            List<Change> changes = service.changes().list(pageToken)
                    .execute().getChanges();

Google推送通知文檔可能已經清楚地提到了這一點,而不是提到請求主體中發生了更改,這就是造成混淆的原因

您可能需要查看文檔- 推送通知 ,它描述了如何使用推送通知在資源更改時通知您的應用程序。

觀看回應

如果監視請求成功創建了一個通知通道,它將返回HTTP 200 OK狀態代碼。

監視響應的消息正文提供有關您剛剛創建的通知通道的信息,如以下示例所示。

{
  "kind": "api#channel",
  "id": "01234567-89ab-cdef-0123456789ab"", // ID you specified for this channel.
  "resourceId": "o3hgv1538sdjfh", // ID of the watched resource.
  "resourceUri": "https://www.googleapis.com/drive/v3/files/o3hgv1538sdjfh", // Version-specific ID of the watched resource.
  "token": "target=myApp-myFilesChannelDest", // Present only if one was provided.
  "expiration": 1426325213000, // Actual expiration time as Unix timestamp (in ms), if applicable.
}

並且如果您將檢查了解通知消息格式:

文件和更改的通知消息為空。

該文檔還提供了示例:

更改“文件”資源的通知消息,其中不包括請求正文:

POST https://example.com/notifications // Your receiving URL.
Content-Type: application/json; utf-8
Content-Length: 0
X-Goog-Channel-ID: 4ba78bf0-6a47-11e2-bcfd-0800200c9a66
X-Goog-Channel-Token: 398348u3tu83ut8uu38
X-Goog-Channel-Expiration: Tue, 19 Nov 2013 01:13:52 GMT
X-Goog-Resource-ID:  ret08u3rv24htgh289g
X-Goog-Resource-URI: https://www.googleapis.com/drive/v3/files/ret08u3rv24htgh289g
X-Goog-Resource-State:  update
X-Goog-Changed: content,properties
X-Goog-Message-Number: 10

變更資源的變更通知消息,包括請求正文:

POST https://example.com/notifications // Your receiving URL.
Content-Type: application/json; utf-8
Content-Length: 118
X-Goog-Channel-ID: 8bd90be9-3a58-3122-ab43-9823188a5b43
X-Goog-Channel-Token: 245t1234tt83trrt333
X-Goog-Channel-Expiration: Tue, 19 Nov 2013 01:13:52 GMT
X-Goog-Resource-ID:  ret987df98743md8g
X-Goog-Resource-URI: https://www.googleapis.com/drive/v3/changes
X-Goog-Resource-State:  changed
X-Goog-Message-Number: 23

{
  "kind": "drive#changes"
}

了解雲端硬盤API通知事件

本部分提供有關在Drive API中使用推送通知時可以接收的通知消息的詳細信息。

您可以在Push Notifications Playground嘗試以下任何事件,或從GitHub下載源。

希望這些信息對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM