发布到GoogleCloudMessaging会返回400 InvalidTokenFormat [英] Post to GoogleCloudMessaging returns 400 InvalidTokenFormat

查看:135
本文介绍了发布到GoogleCloudMessaging会返回400 InvalidTokenFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发布到 Google Cloud Messaging Api(GCM),但我的请求失败,响应 HTTP / 1.1 400 InvalidTokenFormat

然而,如果我改变我的程序以便它连接而不是本地主机,而且我只是通过将请求传递给其他将请求传输到GCM的请求,请求成功。下面是失败的代码:

  import java.net.URL; 
导入java.net.HttpURLConnection;
import java.io.OutputStream;

public class GcmPostMe {
public static void main(String [] args){

String data ={\to \:\ ***被审查的收件人标记*** \};
String type =application / json;
尝试{
网址u =新网址(https://android.googleapis.com/gcm/send/);
HttpURLConnection conn =(HttpURLConnection)u.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod(POST);
conn.setRequestProperty(Authorization,key =+*** censored api key ***);
conn.setRequestProperty(Content-Type,type);
conn.setRequestProperty(Content-Length,String.valueOf(data.length()));
OutputStream os = conn.getOutputStream();
os.write(data.getBytes());
System.out.println(conn.getResponseCode()++ conn.getResponseMessage());
conn.disconnect();
} catch(Exception e){
System.err.println(出错了);
}
}
}

上述代码中的网址为 http:// localhost:10000 / gcm / send ,然后执行



  nc -l 10000 | sed -es / localhost:10000 / android.googleapis.com /| openssl s_client -connect android.googleapis.com:443 

在我运行程序之前。

解决方案

好的,我发现我的错误:路径错误,尾随 / 在某种程度上使它无法工作。



执行HTTP POST以 https://android.googleapis.com/gcm/send/ 为您提供HTTP / 1.1 400 InvalidTokenFormat



http://android.googleapis.com/gcm/send (不带结尾符/)成功使用HTTP / 1.1 200 OK



以下工作:

  import java.net.URL; 
导入java.net.HttpURLConnection;
import java.io.OutputStream;

public class GcmPostMe {
public static void main(String [] args){

String data ={\to \:\ ***被审查的收件人标记*** \};
String type =application / json;
尝试{
网址u =新网址(https://android.googleapis.com/gcm/send);
HttpURLConnection conn =(HttpURLConnection)u.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod(POST);
conn.setRequestProperty(Authorization,key =+*** censored api key ***);
conn.setRequestProperty(Content-Type,type);
conn.setRequestProperty(Content-Length,String.valueOf(data.length()));
OutputStream os = conn.getOutputStream();
os.write(data.getBytes());
System.out.println(conn.getResponseCode()++ conn.getResponseMessage());
conn.disconnect();
} catch(Exception e){
System.err.println(出错了);
}
}
}


I'm trying to post to Googles Cloud Messaging Api (GCM), but my request fails with response HTTP/1.1 400 InvalidTokenFormat.

However, if I change my program so that it connects to localhost instead, and I simply pipe the request it makes through to something else that transmits the request to GCM, the request succeeds. Below is the code that fails:

import java.net.URL;
import java.net.HttpURLConnection;
import java.io.OutputStream;

public class GcmPostMe {
    public static void main (String[] args) {

        String data = "{\"to\":\" *** censored recipient token *** \"}";
        String type = "application/json";
        try {
            URL u = new URL("https://android.googleapis.com/gcm/send/");
            HttpURLConnection conn = (HttpURLConnection) u.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty( "Authorization", "key=" + " *** censored api key *** " );
            conn.setRequestProperty( "Content-Type", type );
            conn.setRequestProperty( "Content-Length", String.valueOf(data.length()));
            OutputStream os = conn.getOutputStream();
            os.write(data.getBytes());
            System.out.println(conn.getResponseCode() + " " + conn.getResponseMessage() );
            conn.disconnect();
        } catch (Exception e) {
            System.err.println("Something went wrong");
        }
    }
}

It works when I change the URL in the code above to "http://localhost:10000/gcm/send" and do

nc -l 10000 | sed -e "s/localhost:10000/android.googleapis.com/" | openssl s_client -connect android.googleapis.com:443

before I run the program.

解决方案

Ok, I've found my mistake: the path was wrong, the trailing / in the path somehow makes it not work.

Doing HTTP POST to https://android.googleapis.com/gcm/send/ gives you HTTP/1.1 400 InvalidTokenFormat

Doing the same POST to http://android.googleapis.com/gcm/send (without the trailing /) succeeds with HTTP/1.1 200 OK

The following works:

import java.net.URL;
import java.net.HttpURLConnection;
import java.io.OutputStream;

public class GcmPostMe {
    public static void main (String[] args) {

        String data = "{\"to\":\" *** censored recipient token *** \"}";
        String type = "application/json";
        try {
            URL u = new URL("https://android.googleapis.com/gcm/send");
            HttpURLConnection conn = (HttpURLConnection) u.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty( "Authorization", "key=" + " *** censored api key *** " );
            conn.setRequestProperty( "Content-Type", type );
            conn.setRequestProperty( "Content-Length", String.valueOf(data.length()));
            OutputStream os = conn.getOutputStream();
            os.write(data.getBytes());
            System.out.println(conn.getResponseCode() + " " + conn.getResponseMessage() );
            conn.disconnect();
        } catch (Exception e) {
            System.err.println("Something went wrong");
        }
    }
}

这篇关于发布到GoogleCloudMessaging会返回400 InvalidTokenFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆