【问题标题】:Android. C2DM Received Authentication Challenge is Null Exception安卓。 C2DM 收到的身份验证质询为空异常
【发布时间】:2012-05-09 22:42:44
【问题描述】:

我正在尝试使用 google c2dm 在 android 设备上发送推送通知,但我遇到了一个小问题。 当我尝试发送消息时,我收到此异常“java.IOException Received Authentication Challenge is Null”,我不知道自己做错了什么。我正在使用Vogella tutorial 发送 c2dm。

public static String getToken(String email, String password)
        throws IOException {
    // Create the post data
    // Requires a field with the email and the password
    StringBuilder builder = new StringBuilder();
    builder.append("Email=").append(email);
    builder.append("&Passwd=").append(password);
    builder.append("&accountType=GOOGLE");
    builder.append("&source=MyLittleExample");
    builder.append("&service=ac2dm");

    // Setup the Http Post
    byte[] data = builder.toString().getBytes();
    URL url = new URL("https://www.google.com/accounts/ClientLogin");
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setUseCaches(false);
    con.setDoOutput(true);
    con.setRequestMethod("POST");
    con.setRequestProperty("Content-Type",
            "application/x-www-form-urlencoded");
    con.setRequestProperty("Content-Length", Integer.toString(data.length));

    // Issue the HTTP POST request
    OutputStream output = con.getOutputStream();
    output.write(data);
    output.close();

    // Read the response
    BufferedReader reader = new BufferedReader(new InputStreamReader(
            con.getInputStream()));
    String line = null;
    String auth_key = null;
    while ((line = reader.readLine()) != null) {
        if (line.startsWith("Auth=")) {
            auth_key = line.substring(5);
        }
    }

    // Finally get the authentication token
    // To something useful with it
    return auth_key;
}

身份验证令牌很好,并返回 200 代码。但是发送消息失败

public static int sendMessage(String auth_token, String registrationId,
        String message) throws IOException {

    StringBuilder postDataBuilder = new StringBuilder();
    postDataBuilder.append(PARAM_REGISTRATION_ID).append("=")
            .append(registrationId);
    postDataBuilder.append("&").append(PARAM_COLLAPSE_KEY).append("=")
            .append("0");
    postDataBuilder.append("&").append("data.payload").append("=")
            .append(URLEncoder.encode(message, UTF8));

    byte[] postData = postDataBuilder.toString().getBytes(UTF8);

    // Hit the dm URL.

    URL url = new URL("https://android.apis.google.com/c2dm/send");
    HttpsURLConnection
            .setDefaultHostnameVerifier(new CustomizedHostnameVerifier());
    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
    conn.setDoOutput(true);
    conn.setUseCaches(false);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type",
            "application/x-www-form-urlencoded;charset=UTF-8");
    conn.setRequestProperty("Content-Length",
            Integer.toString(postData.length));
    conn.setRequestProperty("Authorization", "GoogleLogin auth="
            + auth_token);

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

    int responseCode = conn.getResponseCode();
    return responseCode;
}

据我从其他帖子中了解到,此错误表示 401 代码,但我无法理解其原因,身份验证令牌很好。

【问题讨论】:

  • 我只是尝试使用 curl 命令发送消息并获得 200 代码,但推送消息未到达我的设备。我不知道为什么。之后我再次尝试从我的代码发送推送,也得到了代码 200。

标签: android authentication android-c2dm


【解决方案1】:

使用的 URL 应该是 https://android.clients.google.com/c2dm/send,而不是 https://android.apis.google.com/c2dm/send。另外,请确保 PARAM_REGISTRATION_ID 是一个等于 registration_id 的字符串,而不是 registrationId 或类似的字符串。

【讨论】:

    猜你喜欢
    • 2013-04-03
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多