-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding support to automatically set the auth token if google requests it
- Loading branch information
Sam Lown
authored and
Olivier Poitrey
committed
Apr 2, 2012
1 parent
b34c09a
commit f2038e7
Showing
2 changed files
with
47 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ See [Google Client Login documentation][2] for details. | |
var config = { | ||
user: '[email protected]', | ||
password: 'your-huge-very-very-strong-password', | ||
source: 'yourCompany-yourMegaApp-version', | ||
source: 'com.company.app-name', | ||
}; | ||
var c2dm = new C2DM(config); | ||
|
||
|
@@ -39,19 +39,23 @@ See [C2DM documentation][3] for details. | |
|
||
var message = { | ||
registration_id: 'Device registration id', | ||
collapse_key: 'Collapse key', | ||
collapse_key: 'Collapse key', // required | ||
'data.key1': 'value1', | ||
'data.key2': 'value2', | ||
delay_while_idle: '1' // remove if not needed | ||
}; | ||
|
||
c2dm.send(message, function(err, messageId){ | ||
|
||
if (err) { | ||
console.log("Something has gone wrong!"); | ||
} else { | ||
console.log("Sent with message ID: ", messageId); | ||
} | ||
}); | ||
|
||
### Avoiding login procedure | ||
You can avoid login procedure by manually setting Google ClientLogin Auth tokin in config for connection. | ||
First of all you need to get this token by executing next command after replacing `ROLE_EMAIL`, `ROLE_PASSWORDPASS` and `YOURCOMPANY-YOURAPP-Version` with your data: | ||
You can avoid the login procedure by manually setting Google ClientLogin Auth token in the connection config. | ||
First of all you need to fetch the token by executing the following command. Replace `ROLE_EMAIL`, `ROLE_PASSWORDPASS` and `YOURCOMPANY-YOURAPP-Version` with your data: | ||
|
||
$ curl -X POST https://www.google.com/accounts/ClientLogin -d Email=ROLE_EMAIL -d Passwd=ROLE_PASSWORDPASS -d accountType=HOSTED_OR_GOOGLE -d service=ac2dm -d source=YOURCOMPANY-YOURAPP-Version | ||
|
||
|
@@ -62,6 +66,34 @@ You will receive three lines. Skip `SID` and `LSID` and copy line starting with | |
}; | ||
var c2dm = new C2DM(config); | ||
|
||
However, Google will occaisionally require the token to be updated before further requests can be accepted. If this happens, the `Update-Client-Auth` header will automatically be used to update the current configurations token, but obviously will not be stored on the next reload. A `token` event will be triggered if this happens so you can update a local configuration file, but its probably easier to perform a login each time the application is started: | ||
|
||
c2dm.on('token', function(err, token) { | ||
// Do something with the new token | ||
}); | ||
|
||
### Testing sending a message | ||
If it looks like things aren't going your way, try sending a request manually using the following curl command and see what google sends back to you: | ||
|
||
$ curl -H "Authorization: GoogleLogin auth=YOUR-LONG-TOKEN" -X POST https://android.apis.google.com/c2dm/send -d "registration_id=DESTINATION-TOKEN" -d "collapse_key=key" -d "data.event=hire" -v | ||
|
||
You should get back the header details along with a message id if sending has been successful. For example: | ||
|
||
.... sending headers and SSL stuff ..... | ||
< HTTP/1.1 200 OK | ||
< Content-Type: text/plain | ||
< Date: Wed, 18 Jan 2012 11:18:01 GMT | ||
< Expires: Wed, 18 Jan 2012 11:18:01 GMT | ||
< Cache-Control: private, max-age=0 | ||
< X-Content-Type-Options: nosniff | ||
< X-Frame-Options: SAMEORIGIN | ||
< X-XSS-Protection: 1; mode=block | ||
< Server: GSE | ||
< Transfer-Encoding: chunked | ||
< | ||
id=0:1326885481081626%900b347100019e5f | ||
.... more SSL stuff .... | ||
|
||
### Keep-alive connection | ||
This module supports Connection: keep-alive header too keep connection to c2dm gate established. You could use it by simply including property in config: | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters