【问题标题】:C2DM - error response from Google endpoint: MismatchSenderIdC2DM - 来自 Google 端点的错误响应:MismatchSenderId
【发布时间】:2012-03-22 19:33:38
【问题描述】:

我正在尝试创建一个使用 c2dm 服务的应用程序。

所以。首先,我使用谷歌电子邮件/密码从 c2dm 服务器获取应用程序的身份验证密钥。

android 应用程序使用不同的电子邮件向 c2dm 服务器注册以获取注册 ID 并将 id 发送给我(到应用程序)

我使用我的身份验证密钥和设备的注册 ID 发送 c2dm。所以 authentication-key 和 registration-id 是使用两个不同的电子邮件生成的。

当我发送 c2dm 时,我收到此错误:God error response from Google datamessaging endpoint: MismatchSenderId

以下是我的代码:

public class C2DMTest 
{
    public static void main(String... args) throws Exception 
    {
        //authentcation using email1
        String auth = "DQAAALoAAADu5rxXlYkYecVYIYKywIkvhhb70SMXgniYqeCNt15i70aO6wmuPBrAYZMMort0z1InHsjblB_R9sLR0wa6mD1CEM9IZH_POhC9iGLKsBO9EBremJJzTCzmj9EVCCWv3-SxYAOJ1CCKVNE7x9oHAehTSapBYWwk80ZjQ19rC2Q8BRctF3aPDLbZBbjyq6rQjUbEQU1vA_WTbgwyENSmNOT_GtTaEUCYtmKauRoOW6TV-LxTWTsvzUApCclSqovcXlk";
        sendMessage(auth);
    }

    /**
     * Send message to mobile device.
     */
    @SuppressWarnings("deprecation")
    public static void sendMessage(String cl) throws IOException 
    {

        String key = "APA91bGgIm0_yjPvr_Wao20XRMJP2HhQSjY2FL9O56CwL73UHqPg_TUrux_voUy3pi_k1aKt1MUt0SkpKCANpC_iw1LQ0szkW9Bs-TFv5lBNyvZZgCBW3H9oIbpQLkz0bUspIY4KqHjaDnaIqSelQLbEbolLQPwHscIQDRIul_W9YVNUTiOhDr8";

        StringBuilder postDataBuilder = new StringBuilder();
        postDataBuilder.append("registration_id").append("=").append(key);
        postDataBuilder.append("&").append("collapse_key").append("=").append("0");
        postDataBuilder.append("&").append("data.payload").append("=").append(URLEncoder.encode("test-content", UTF8));
        byte[] postData = postDataBuilder.toString().getBytes(UTF8);

        URL url = new URL("https://android.apis.google.com/c2dm/send");

        HostnameVerifier hVerifier = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession
                    session) {
                return true;
            }
        };

        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setHostnameVerifier(hVerifier);


        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length", Integer.toString(postData.length));
        conn.setRequestProperty("Authorization", "GoogleLogin auth="+cl);

        //------------------
        OutputStream out = conn.getOutputStream();
        //--------------------------
        out.write(postData);
        out.close();

        int sw = conn.getResponseCode();
        System.out.println("" + sw);
        switch (sw) 
        {
            case 200:
                System.out.println("Success, but check for errors in the body");
                break;
            case 503:
                System.out.println("Service unavailable");
                break;
            case 401:
                System.out.println(" Invalid authentication token");
                break;
        }


        String responseLine = new BufferedReader(new InputStreamReader(conn.getInputStream())).readLine();
        String[] responseParts = responseLine.split("=", 2);

        if (responseParts[0].equals("id")) 
        {
            System.out.println("Successfully sent data message to device: "+ responseLine);
        }

        if (responseParts[0].equals("Error")) 
        {
            String err = responseParts[1];
            System.out.println("Got error response from Google datamessaging endpoint: "+ err);
            throw new IOException(err);
        }




    }
}

请告诉我为什么会收到此错误消息。提前非常感谢。

【问题讨论】:

    标签: google-app-engine android-c2dm


    【解决方案1】:

    确保为sender 意图额外字段和服务器使用相同的电子邮件。因此,您的服务器身份验证值必须来自您在客户端用作sender 的同一电子邮件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-28
      • 2016-07-31
      • 2021-08-28
      • 1970-01-01
      • 2018-06-26
      • 1970-01-01
      相关资源
      最近更新 更多