java - 使用 Java 客户端库的 Google Subscription Defer 会出现 500 Internal Server Error

标签 java google-app-engine google-api-java-client

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() 工作,它提供 token 、购买时间和其他详细信息,但 defer 给出

    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) 

最佳答案

它在测试帐户中出现问题,但是当我切换到真实帐户时问题就解决了。

关于java - 使用 Java 客户端库的 Google Subscription Defer 会出现 500 Internal Server Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29964537/

相关文章:

java - HttpJUnitRunner 无法解析为类型

java - AdView 不再具有 setGravity?

python - 如何在GAE中的NDB中执行投影查询

java - 使用 - 用于 Java 的 Google API 客户端库检索 Youtube 上观看次数最多、评价最多的视频详细信息

java - 为什么我们在Java中不需要很长的后缀?

java - 如何创建视频的缩略图?

python - 从Python客户端访问后端API

google-app-engine - 如何在Google App Engine中使用XMPP协议(protocol)获取聊天消息发送者的IP地址?

java - Android Calendar API 插入重复事件

android - GoogleApiClient 似乎没有对 disconnect() API 的回调...?