【发布时间】:2014-04-09 04:44:42
【问题描述】:
我正在尝试使用 GCM 向设备发送消息。作为一种特殊情况,我使用 android 设备作为我的第三方服务器。我添加了以下代码,但我收到 "Unauthorized Error 401" 。这里我只是想在android中复制php服务器代码。
无法运行的 JAVA 代码 - 返回错误 401。
// HTTP POST request
private void sendPost() throws JSONException, ClientProtocolException, IOException{
final String SERVICE_URL = "https://android.googleapis.com/gcm/send";
InputStream inputStream = null;
String result = "";
final String REGISTRATION_ID ="APA91bHH4iNCFdWUIXSHRXV3hsBeF8IU0ZElts9AXaHItDfRdRld-kwkVx69EFYZePPuLOW1hTkUCmAwyTeGdoirr25KJ3RG1AikGbBzsvqaPCLLz9YYCwPDuB6xUupVKmllNoTn2v0BRTTkC6OS_i8zerATtBP3gg" ;
final String API_KEY = "AIzaSyARQTvQ5pRYEbW-9V98uDHNnn10Rwffx18";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(SERVICE_URL);
int iresponse;
sds
String base64EncodedCredentials = Base64.encodeToString(API_KEY.getBytes("UTF-8"), Base64.NO_WRAP);
// inform the server about the type of the content
httpPost.addHeader("Authorization", "key=" + base64EncodedCredentials);
String json = "";
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("registration_ids", REGISTRATION_ID);
// convert JSONObject to JSON to String
json = jsonObject.toString();
// set json to StringEntity
StringEntity se = new StringEntity(json);
// set httpPost Entity
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
// Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
iresponse = httpResponse.getStatusLine().getStatusCode();
System.out.println(iresponse);
// receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// convert inputstream to string
if(inputStream != null)
result = convertInputStreamToString(inputStream);
System.out.println(result);
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException{
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
工作 PHP 代码
<html>
<head>
<title>Online PHP Script Execution</title>
</head>
<body>
<?php
$api_key = "AIzaSyARQTvQ5pRYEbW-9V98uDHNnn10Rwffx18";
$registrationIDs = array("APA91bHH4iNCFdWUIXSHRXV3hsBeF8IU0ZElts9AXaHItDfRdRld-kwkVx69EFYZePPuLOW1hTkUCmAwyTeGdoirr25KJ3RG1AikGbBzsvqaPCLLz9YYCwPDuB6xUupVKmllNoTn2v0BRTTkC6OS_i8zerATtBP3gg") ;
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
'registration_ids' => $registrationIDs,
'data' => array( "message" => "Hi" ),
);
$headers = array(
'Authorization: key=' . $api_key,
'Content-Type: application/json');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER , false );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST , false );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
</body>
</html>
在上面的 Java 代码中,API_KEY 是浏览器密钥,而 REGISTRATION_ID 是 Google Cloud Server 返回的 id。使用服务器密钥测试同样的事情。
【问题讨论】:
-
首先,永远不要像这样分享你的 API_KEY。
-
@Kedarnath。以后我不会了。这是一个测试应用程序。一段时间后我会删除它。你能帮我找出我在上面的java代码中做错了什么吗?
标签: java php android https google-cloud-messaging