Cian
Cian

Reputation: 1629

No Token Returned With Drive Push Notification

I'm creating a notification channel for users of a web app like this:

channel_id = str(uuid.uuid4())
channel_type = 'web_hook'
channel_address = 'https://example.com/drive'
current_time_ms = int(round(time.time() * 1000))
    body = {
        'id': channel_id,
        'type': channel_type,
        'address': channel_address, 
        'token': username, 
        'expiration': (current_time_ms + 604800000) # expire 1 week from now
    }
response = user_drive_service.changes().watch(body=body).execute()
expiration_time = response['expiration']
print "Drive push channel created for " + username

I get a response like this when it's created

{
    u'resourceUri': u'https://www.googleapis.com/drive/v2/changes?alt=json&includeDeleted=true&includeSubscribed=true&maxResults=100&alt=json', 
    u'kind': u'api#channel', 
    u'resourceId': u'9876lkjhg4321asdf', 
    u'token': u'a_username', 
    u'expiration': u'1407921041000', 
    u'id': u'123456abcd789efgh'
}

Then I'm getting notifications like

{
    u'kind': u'drive#change', 
    u'id': u'123456', # Doesn't match ID in response from channel creation...
    u'selfLink': u'https://www.googleapis.com/drive/v2/changes/123456'
}

but the documentation here and here says the token field should be returned to me, so I can pull that username from it. Can anyone point out where the issue could be here?

Upvotes: 0

Views: 91

Answers (1)

Cian
Cian

Reputation: 1629

So, turned out to be basic mistake on my behalf. In case anyone's wondering - I was trying to get the token like this

result = json.loads(request.data)
user_id = result['token']

The result I posted in the question was the output of request.data. I should have been doing

token = request.headers.get('X-Goog-Channel-Token')

Tokens, expiration times, resource ID's etc. are all in the header. Oops!

Upvotes: 1

Related Questions