2

私は自分の携帯電話にメッセージを送るのに疲れています。ブラウザを介して、この操作を実行するメソッドを呼び出し、registrationId、authTokenなどをログに記録しました。これは、ローカルサーバーでテストし、これらのキーを使用してメッセージが電話に送信されたためです。

ただし、App Engineでは、urlfetch.fetchfor' https://android.clients.google.com/c2dm/send'の結果に401エラーがあります。

または、これが認証の問題である場合。メソッドが呼び出され、App Engineサーバーのメソッドの最後でエラーが発生するため、上記の問題ではないかと思います。

C2DMサーバーにリクエストを送信する方法は次のとおりです。

params = {
          'registration_id':registrationId,
          'collapse_key':0,
          'data.payload':encoded_msg
         }

        paramsByte = urllib.urlencode(params)
        logging.info(registrationId)

        url = 'https://android.clients.google.com/c2dm/send'
        logging.info(token)
        result = urlfetch.fetch(url=url,
                                payload=paramsByte,
                                method=urlfetch.POST,
                                headers={'Content-Type':'application/x-www-form-urlencoded',
                                         'Authorization':'GoogleLogin auth='+token}
                                )

どんな助けでもいただければ幸いです。ありがとう。


アップデート

これで、クライアントは提案どおりにホスティングサーバーで実行され、「https://android.clients.google.com/c2dm/send」に接続すると401エラーが発生します。

ただし、同じトークンとregIdを使用して端末で次のコマンドを使用すると、機能します。

curl --header "認証:GoogleLogin auth = your_authenticationid" "https://android.apis.google.com/c2dm/send" -d register_id = your_registration -d "data.payload =payload" -dcollapse_key = 0

サーバーでメソッドを呼び出すクライアントコード:

$.getJSON('http://myapp.appspot.com/method?userId='+userId+'&message='+theMessage+'&callback=?', 
    function(data)
    {
        console.log(data);
    });

サーバーの完全なメソッドコード:

class PushHandler(webapp.RequestHandler):

    '''This method sends the message to C2DM server to send the message to the phone'''
    def get(self):
        logging.info('aqui dentro')
        userId = self.request.get('userId')
        message = self.request.get('message')
        callback = self.request.get('callback')
        token = getToken(self) #this is a method I've implemented to get the token from C2DM servers by passing the SenderId and Password
        registrationId = ''
        contactNumber = ''

        # Get the registrationId to send to the C2DM server to know which 
        # device it may send the message
        regQuery = C2DMUser.all()
        regQuery.filter('userId =', int(userId))
        for k in regQuery:
            registrationId = k.registrationId

        # Builds the json to be sent to the phone
        record_to_json = {
                          'userId':userId,
                          'message':message
                          }
        data = []
        data.append(record_to_json)
        jsondata = simplejson.dumps(data) # Creates the json

        # Encode the JSON String
        u = unicode(jsondata, "utf-8")
        encoded_msg = u.encode("utf-8")

        params = {
                  'registration_id':registrationId,
                  'collapse_key':0,
                  'data.payload':encoded_msg
                  }

        paramsByte = urllib.urlencode(params)

        url = 'https://android.clients.google.com/c2dm/send'
        logging.info(token)
        result = urlfetch.fetch(url=url,
                                payload=paramsByte,
                                method=urlfetch.POST,
                                headers={'Content-Type':'application/x-www-form-urlencoded',
                                         'Authorization':'GoogleLogin auth='+token}
                                )

        data = []
        params_key = { 'status_code':result.status_code }
        data.append(params_key)
        self.response.headers['Content-Type'] = 'application/json'
        jsondata = simplejson.dumps(data)

        if result.status_code == 200:
            logging.info(result.status_code)
            self.response.out.write('' + callback + '(' + jsondata + ')') # handle the JSONP
        else:
            logging.info(result.status_code)
            self.response.out.write(result.status_code)
4

2 に答える 2

1

コードのパッケージ名は、c2dm アカウントにサインアップしたときに指定したものと一致する必要があります。Java の場合、サインアップ時に com.myapp を指定した場合、c2dm 呼び出しはそのパッケージ内で発生する必要があります。ただし、これが Python にどのように変換されるかはわかりません。

于 2012-05-18T19:47:30.223 に答える
0

C2DM の部分に関する限り、すべて問題ないようです。同じ資格情報を使用してローカル サーバーで動作すると言っている場合は、App Engine でも動作するはずだと推測しています。

XMLHttpRequest エラーに関する限り、XMLHttpRequest を介して他のドメインまたはサブドメインにリクエストを発行することはできません。したがって、localhost から にリクエストを発行することはできませんyourSite解決策はJSONPを使用することです。

于 2012-05-09T12:04:03.947 に答える