[Answer]-Django c2dm with OAuth2

1👍

I suppose you’ve been able to perform step 2 Exchange authorization code for tokens on Google OAuth 2.0 Playground. Then you should have acquired a refresh token and an access token (if you didn’t receive a refresh token, verify that you have checked Force approval prompt and selected offline for Access type in the OAuth 2.0 Configuration.

The access token will expire after some time (usually 1 hour), but the refresh token does not. The refresh token (together with the OAuth Client ID and the OAuth Client secret) can be used to obtain a new access token:

curl --data-urlencode "client_id=OAuthClientID"
     --data-urlencode "client_secret=OAuthClientSecret"
     --data-urlencode "refresh_token=RefreshToken"
     -d "grant_type=refresh_token" "https://accounts.google.com/o/oauth2/token"

(Replace OAuthClientID, OAuthClientSecret, RefreshToken). For futher reading refer to: Using OAuth 2.0 for Web Server Applications – Offline Access

Now you can use this access token and the registration ID of the device to send messages to that device using C2DM:

curl -k -H "Authorization: Bearer AccessToken"
     --data-urlencode "registration_id=RegistrationID"
     --data-urlencode "collapse_key=0"
     --data-urlencode "data.message=YourMessage"
     "https://android.apis.google.com/c2dm/send"

(Replace AccessToken, RegistrationID and YourMessage)

Leave a comment