1
oauth 인증없이 google에서 c2dm을 사용하는 응용 프로그램 버전을 가지고 있으며 정상적으로 작동합니다. 하지만 oauth 2로 마이그레이션하고 싶습니다. 메시지를 보내려고 할 때 문제가 있습니다. 401이 무단으로 계속 수신됩니다. ClientID, 전자 메일 주소 및 비공개 키를 가져 오기 위해 서비스 계정을 만들었습니다. 이미 c2dm (두 번)에 가입하고 인증 토큰을 여러 번 새로 고칩니다. 서버와 장치에서 동일한 전자 메일을 사용하고 있습니다. 왜 이런 일이 일어나는 지 아는 사람이 있습니까?oauth 2 및 401 받기와 함께 c2dm에게 메시지 보내기
public static String getToken() {
JsonFactory jsonFactory = new JacksonFactory();
HttpTransport httpTransport = new NetHttpTransport();
String clientId = "****.apps.googleusercontent.com";
String pkcs12Repo = "*****privatekey.p12";
String scope = "https://android.apis.google.com/c2dm";
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(clientId)
.setServiceAccountPrivateKeyFromP12File(new File(pkcs12Repo))
.setServiceAccountScopes(scope)
.build();
credential.refreshToken();
String token = credential.getAccessToken();
return token;
}
public static int sendMessage(String auth_token, String registrationId, String message) {
StringBuilder postDataBuilder = new StringBuilder();
postDataBuilder.append("registration_id").append("=").append(registrationId);
postDataBuilder.append("&").append("collapse_key").append("=").append("0");
postDataBuilder.append("&").append("data.payload").append("=").append(URLEncoder.encode(message, "UTF-8"));
byte[] postData = postDataBuilder.toString().getBytes("UTF-8");
URL url = new URL("https://android.clients.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", "Bearer " + auth_token);
OutputStream out = conn.getOutputStream();
out.write(postData);
out.close();
int responseCode = conn.getResponseCode();
return responseCode;
}
많은 사람들이 이미 OAuth2와 함께 C2DM을 성공적으로 사용했습니다. Google은 최근 OAuth2.0을 통합하기 위해이 서비스를 변경했으며 ClientLogin AuthToken은 더 이상 사용되지 않습니다. – Corgan