我在这里使用我的浏览器密钥向gcm服务器发送消息。也尝试使用服务器密钥,但仍然得到相同的错误。我还尝试使用defaulthttpclient
使用用户名和密码进行身份验证,但没有得到任何结果。
这是我的应用服务器的代码:
package com.example.gcmserver;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.AuthState;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;
public class Server {
public static void main(String[] args) throws IOException {
String url = "https://android.googleapis.com/gcm/send";
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("username", "password"));
client.addRequestInterceptor(new HttpRequestInterceptor() {
@Override
public void process(HttpRequest arg0, HttpContext context)
throws HttpException, IOException {
// TODO Auto-generated method stub
AuthState state = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
if (state.getAuthScheme() == null) {
BasicScheme scheme = new BasicScheme();
CredentialsProvider credentialsProvider = (CredentialsProvider) context.getAttribute(ClientContext.CREDS_PROVIDER);
Credentials credentials = credentialsProvider.getCredentials(AuthScope.ANY);
if (credentials == null) {
throw new HttpException();
}
state.setAuthScope(AuthScope.ANY);
state.setAuthScheme(scheme);
state.setCredentials(credentials);
}}
}, 0);
HttpPost post = new HttpPost(url);
HttpPost httppost = new HttpPost("https://android.googleapis.com/gcm/send");
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("data.registration id","APA91bGChJVdx2L1AUIJEVIBBVEkxEAk6zixj5V-L--fDGXudvrtl5gSQDgAP4iUJoMRq57sLSpGtsSQ5gnprX4McOPq7WxglKeQyTmaysVb0r-adOZ4tTjWG2MYwVEayFKy9Rwf1mloVt2jb-_izUdN-SfKvlpUpEYXNNT1987tYASKCcam16g"));
urlParameters.add(new BasicNameValuePair("data.title", "e-pass application"));
urlParameters.add(new BasicNameValuePair("data.message", "Your code is 13133"));
httppost.setHeader("Authorization",
"key=AIzaSyDANlJHGPmzo1so_yyqkYZO7LSrQpnR7mk");
httppost.setHeader("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
post.setEntity(new UrlEncodedFormEntity(urlParameters, "UTF-8"));
HttpResponse response = client.execute(post);
System.out.println("Response Code : "
+ response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
}
}
I have also add the httpclient, httpcore jar files in build path but still getting error.
答案 0 :(得分:0)
有几种可能的原因导致响应代码401,例如项目ID中的错误。你可能已经忘记了在api控制台中启用云消息服务。
您可以在here获取更多详细信息:)
答案 1 :(得分:0)
在java中使用基础推送通知代码
here is already a collection named "users"