C2DM - 来自Google端点的错误响应:MismatchSenderId

时间:2012-03-21 19:53:13

标签: google-app-engine android-c2dm

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

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

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

我使用我的身份验证密钥和设备的注册ID来发送c2dm。因此,身份验证密钥和注册ID是使用两个不同的电子邮件生成的。

当我发送c2dm时,收到此错误:来自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);
        }




    }
}

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

1 个答案:

答案 0 :(得分:3)

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