我试图用餐厅书写服务器侧码,以发送电文。 我认为,我可以成功地获得授权,但当我利用这一授权发出信息时,总是显示HTTP Error 401:未经许可的。 我的法典在这里是:
import urllib, urllib2
class ClientLoginTokenFactory:
_token = None
def __init__(self):
self.url = https://www.google.com/accounts/ClientLogin
self.accountType = HOSTED_OR_GOOOGLE
self.email = [email protected]
self.pwd = xxxxx
self.service = ac2dm
self.source = com.lei.c2dmtest
def getToken(self):
if self._token is None:
values = { accountType :self.accountType, Email :self.email, Passwd :self.pwd, source :self.source, service :self.service}
data = urllib.urlencode(values)
request = urllib2.Request(self.url,data)
response = urllib2.urlopen(request)
responseAsString = response.read()
#print responseAsString
responseAsList = responseAsString.split(
)
self._token = responseAsList[2].split( = )[1]
print self._token
return self._token
class C2DM:
def __init__(self):
self.url = https://android.apis.google.com/c2dm/send
self.clientAuth = None
self.registrationId = None
self.collapseKey = None
self.data = {}
def sendMessage(self):
if self.registrationId == None or self.collapseKey == None:
return False
clientAuthFactory = ClientLoginTokenFactory()
self.clientAuth = clientAuthFactory.getToken()
print self.clientAuth
values = { registration_id : self.registrationId, collapse_key : self.collapseKey, data.msg : Hello!!! }
#print values
headers = { Authorization : GoolgeLogin auth= + self.clientAuth}
#print headers
data = urllib.urlencode(values)
request = urllib2.Request(self.url, data, headers)
try:
response = urllib2.urlopen(request)
responseAsString = response.read()
return "AAA"
return responseAsString
except urllib2.HTTPError, e:
print HTTPError + str(e)
sender = C2DM()
sender.registrationId = APA91bGZaTpEgJ9a4tOgFTisedsJCOBFvsdwSkKqfzY97QyOiDlzvlZJv6cP1xDENw6DJM3sYTLslGGke0yAMTH9Ep0eeDmfVLUuM2bkFfwKWGyLG4HQDHR1zxw5TN1hoagi5FLWXYKmEQAvX6Qw-rIiwg03UBrvXA
sender.collapseKey = 3
response = sender.sendMessage()
http://strong> RegistrationId也是正确的,我已使用java代码测试并证明该标准有效。 但是,当我把它改为平时,它总是表示“未经许可”
没有人对此有任何想法?
感谢!