English 中文(简体)
C2DM始终表示,假装法“未经许可”
原标题:C2DM always indicates "Unauthorized" for python code

我试图用餐厅书写服务器侧码,以发送电文。 我认为,我可以成功地获得授权,但当我利用这一授权发出信息时,总是显示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代码测试并证明该标准有效。 但是,当我把它改为平时,它总是表示“未经许可”

没有人对此有任何想法?

感谢!

问题回答

页: 1





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签