Submitting google subscription using Java client libraries gives 500 internal server errors
package com.google.play.developerapi.samples;
import java.io.File;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Collections;
import javax.annotation.Nullable;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.repackaged.com.google.common.base.Preconditions;
import com.google.api.client.repackaged.com.google.common.base.Strings;
import com.google.api.services.androidpublisher.AndroidPublisher;
import com.google.api.services.androidpublisher.AndroidPublisher.Purchases;
import com.google.api.services.androidpublisher.AndroidPublisher.Purchases.Subscriptions;
import com.google.api.services.androidpublisher.AndroidPublisher.Purchases.Subscriptions.Defer;
import com.google.api.services.androidpublisher.AndroidPublisher.Purchases.Subscriptions.Get;
import com.google.api.services.androidpublisher.AndroidPublisherScopes;
import com.google.api.services.androidpublisher.model.SubscriptionDeferralInfo;
import com.google.api.services.androidpublisher.model.SubscriptionPurchase;
import com.google.api.services.androidpublisher.model.SubscriptionPurchasesDeferRequest;
public class AndroidPublisherHelper {
private static final Log log = LogFactory.getLog(AndroidPublisherHelper.class);
static final String MIME_TYPE_APK = "application/vnd.android.package-archive";
/** Path to the private key file (only used for Service Account auth). */
private static final String SRC_RESOURCES_KEY_P12 = "resources/key.p12";
/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
/** Global instance of the HTTP transport. */
private static HttpTransport HTTP_TRANSPORT;
private static Credential authorizeWithServiceAccount(String serviceAccountEmail)
throws GeneralSecurityException, IOException {
log.info(String.format("Authorizing using Service Account: %s", serviceAccountEmail));
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(serviceAccountEmail)
.setServiceAccountScopes(
Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER))
.setServiceAccountPrivateKeyFromP12File(new File(SRC_RESOURCES_KEY_P12))
.build();
return credential;
}
/**
* Performs all necessary setup steps for running requests against the API
* using the Installed Application auth method.
*
* @param applicationName the name of the application: com.example.app
* @return the {@Link AndroidPublisher} service
*/
protected static AndroidPublisher init(String applicationName) throws Exception {
return init(applicationName, "abcdefghijk@developer.gserviceaccount.com");
}
/**
* Performs all necessary setup steps for running requests against the API.
*
* @param applicationName the name of the application: com.example.app
* @param serviceAccountEmail the Service Account Email (empty if using
* installed application)
* @return the {@Link AndroidPublisher} service
* @throws GeneralSecurityException
* @throws IOException
*/
protected static AndroidPublisher init(String applicationName,
@Nullable String serviceAccountEmail) throws IOException, GeneralSecurityException {
Preconditions.checkArgument(!Strings.isNullOrEmpty(applicationName),
"applicationName cannot be null or empty!");
// Authorization.
newTrustedTransport();
Credential credential;
credential = authorizeWithServiceAccount(serviceAccountEmail);
// Set up and return API client.
return new AndroidPublisher.Builder(
HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(applicationName)
.build();
}
private static void newTrustedTransport() throws GeneralSecurityException,
IOException {
if (null == HTTP_TRANSPORT) {
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
}
}
public static void main(String args[])
{
try {
AndroidPublisher publisher=init("App payment");
System.out.println(publisher);
Purchases purchases = publisher.purchases();
Subscriptions subscriptions=purchases.subscriptions();
Get get = subscriptions.get("package", "productId", "purchaseToken");
SubscriptionPurchase subscripcion = get.execute(); //Exception returned here
System.out.println(subscripcion);
SubscriptionDeferralInfo deferralInfo =new SubscriptionDeferralInfo();
deferralInfo.setExpectedExpiryTimeMillis(subscripcion.getExpiryTimeMillis());
deferralInfo.setDesiredExpiryTimeMillis(1430837304000l);
SubscriptionPurchasesDeferRequest request= new SubscriptionPurchasesDeferRequest();
request.setDeferralInfo(deferralInfo);
Defer defer=subscriptions.defer("package", "productId", "purchaseToken", request);
System.out.println(defer.executeUnparsed().toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
get.execute () and it gives details like token, purchase time and other details, but deferral gives
com.google.api.client.googleapis.json.GoogleJsonResponseException: 500 Internal Server Error
{
"code" : 500,
"message" : null
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:312)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1049)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
+3
source to share