Cześć,
Mam jakiś serwere zrobiony na Django (python).
Teraz z tego serwerka chcaiłbym wysłać "push notification" na telefon z androidem.
Z telefonu dostaje "registration id" a serwer bez problemu pobiera dla siebie "google autorization token".
Problem mam z wysłaniem samej notyfikacji, serwer C2DM odbija mi z kodem 401 (przez jakiś czas było to 403).
Kod wygląda tak:
def sendAndroidPushNotification(registration_id, collapse_key, a, b) :
try:
auth = getNewAndroidAuthorizationToken() # to dzila token ma dlugosc 267 znakow
push_request = urllib2.Request("https://android.apis.google.com/c2dm/send")
data = urllib.urlencode({'data.testA' : a,
'data.testB' : b,
'collapse_key' : collapse_key,
'registration_id': registration_id
})
push_request.add_data( data )
push_request.add_header('Authorization', 'GoogleLogin auth=' + auth)
push_request.add_header('Content-Type', 'application/x-www-form-urlencoded')
push_request.add_header('Content-Length', len(data))
urllib2.build_opener().open(push_request)
except urllib2.HTTPError as e:
print 'got exception during push notification'
print 'Reason: "{0}" code: {1}'.format(e.reason, e.code)
pass
W konsoli widzę: "Reason: "Unauthorized" code: 401"
Swój kod oparłem o to co znalazłem stackoverflow tam jest odnośnik do kodu.
Wersja kodu z httplib.HTTPSConnection zamist urllib2.Request ma ten sam problem :( .
a tak pobieram token (może źle parsuje ten token?): ```python def getNewAndroidAuthorizationToken() : request = urllib2.Request("https://www.google.com/accounts/ClientLogin")
data = urllib.urlencode({'accountType' : 'HOSTED_OR_GOOGLE',
'Email' : '[email protected]',
'Passwd' : 'smiesznehaslo',
'service' : 'ac2dm',
'source' : 'com.moja.nazwa.paczki',})
request.add_data(data)
content = urllib2.build_opener().open(request)
lines = content.readlines()
for line in lines :
if line.find("Auth=")==0 :
print 'AuthorizationToken found: ' + line[5:]
return line[5:]
print 'FAILED To find Authorization Token'
return