【问题标题】:Using Android Google Play Dev API from Android app从 Android 应用程序使用 Android Google Play Dev API
【发布时间】:2019-06-08 22:14:45
【问题描述】:

我想在 Android 应用中实现 Google Dev API。 我正在尝试使用 Google 为 Java 提供的示例,这里: https://github.com/googlesamples/android-play-publisher-api/tree/master/v3/java/src/com/google/play/developerapi/samples

我只是将它们修改为使用 Android 的 AssetManager 提供 client-secret.json 文件的 InputStream。

然而,由于 java.awt 依赖项,这仍然与 Android 不兼容,如下面的错误所示:

  java.lang.RuntimeException: An error occurred while executing doInBackground()
    at android.os.AsyncTask$3.done(AsyncTask.java:354)
    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
    at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
    at java.util.concurrent.FutureTask.run(FutureTask.java:271)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:764)
 Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Ljava/awt/Desktop;
    at com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp.browse(AuthorizationCodeInstalledApp.java:129)
    at com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp.onAuthorization(AuthorizationCodeInstalledApp.java:113)
    at com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp.authorize(AuthorizationCodeInstalledApp.java:81)
    at net.myapp.AndroidPublisherHelper.authorizeWithInstalledApplication(AndroidPublisherHelper.java:140)

作为记录,第 140 行附近的代码是:

    return new AuthorizationCodeInstalledApp(
            flow, new LocalServerReceiver()).authorize(INST_APP_USER_ID);

其中涉及到依赖:

 import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
 import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;

如何转换这些示例以使其与 Android 兼容。我不知道它们是如何工作的,所以我只是将其复制/粘贴到应该建立连接并列出我的开发 apk 的 AsyncTask 中。

【问题讨论】:

    标签: java android google-play-developer-api


    【解决方案1】:

    您必须通过 play-services-auth 库登录。

    此代码示例可能会对您有所帮助:

    private void requestSignIn( final Context context ) {
        GoogleSignInOptions signInOptions =
                new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                        .requestEmail()
                        .requestScopes(new Scope( AndroidPublisherScopes.ANDROIDPUBLISHER ))
                        .build();
        GoogleSignInClient client = GoogleSignIn.getClient( context, signInOptions );
    
        // The result of the sign-in Intent is handled in onActivityResult.
        startActivityForResult(client.getSignInIntent(), REQUEST_CODE_SIGN_IN);
    }
    
        @Override
    protected void onActivityResult( int requestCode, int resultCode, @Nullable Intent data ) {
        switch (requestCode) {
            case REQUEST_CODE_SIGN_IN:
                if ( resultCode == Activity.RESULT_CANCELED ) {
                    // TODO: login permitted
                    break;
                }
                if ( resultCode == Activity.RESULT_OK && data != null ) {
                    handleSignInResult( data );
                }
                break;
        }
        super.onActivityResult( requestCode, resultCode, data );
    }
    
    private void handleSignInResult( Intent result ) {
        GoogleSignIn.getSignedInAccountFromIntent(result)
                .addOnSuccessListener( googleAccount -> {
                    Log.d("Log", "Signed in as " + googleAccount.getEmail() );
    
                    // Use the authenticated account to sign in to the Drive service.
                    GoogleAccountCredential credential =
                            GoogleAccountCredential.usingOAuth2(
                                    this, Collections.singleton( AndroidPublisherScopes.ANDROIDPUBLISHER ));
                    credential.setSelectedAccount(googleAccount.getAccount());
                    new BackgroundTask().execute( credential );
                })
                .addOnFailureListener(exception -> {
                    Log.e( "Log", "Unable to sign in.", exception);
                });
    }
    
    class BackgroundTask extends AsyncTask<GoogleAccountCredential, Void, Void> {
    
        @Override
        protected Void doInBackground( GoogleAccountCredential... googleAccountCredentials ) {
            main( googleAccountCredentials[0] );
            return null;
        }
    }
    
    
    // Vars from AndroidPublisherHelper
    
    public static void main( GoogleAccountCredential credential) {
        Preconditions.checkArgument(!Strings.isNullOrEmpty(ApplicationConfig.PACKAGE_NAME),
            "applicationName cannot be null or empty!");
    
        credential.setBackOff(new ExponentialBackOff());
    
    
        // Set up and return API client. Create the API service.
        final AndroidPublisher service = new AndroidPublisher.Builder(
            newTrustedTransport(), JSON_FACTORY, credential).setApplicationName(ApplicationConfig.APPLICATION_NAME)
            .build();
    
        final Edits edits = service.edits();
    
        // Create a new edit to make changes.
        Insert editRequest = edits
                .insert( ApplicationConfig.PACKAGE_NAME, // Or other app's package id
                        null /** no content */);
        AppEdit appEdit = editRequest.execute();
    
        // Do what you want with appEdit according to the API
        //.....
    }
    

    【讨论】:

      【解决方案2】:

      警告!

      我认为发布包含此库的应用程序违反了 Google Play 政策通过以下情况(由下面的链接描述):

      不允许第三方使用 API 代表您发布到您的帐户。请勿使用 API 向 Google Play 商店提供创建、上传、发布、分发或更新应用程序的开发人员服务或工具。这样做违反了 Google Play Developer API 服务条款。此类行为可能会导致您的帐户被暂停,并将您的应用从 Google Play 商店中删除。在您的帐户上所做的一切都是您的责任,并且允许第三方使用您的帐户会使您的所有用户面临风险。

      和条款:

      Publishing API:您只能将 Publishing API 用于您自己的与根据 Google Play 开发者分发协议分发产品相关的活动。 您不得利用第三方的开发者帐户访问 Publishing API,使用 Publishing API 通过开发者工具或服务代表第三方发布应用程序,或共享您自己的开发者帐户以允许第三方代表您访问 Publishing API。 违反此规定可能会导致暂停、终止对 Publishing API、您的开发者帐户的访问,和/或暂停或终止创建、上传、发布的第三方应用、分发或更新违反本规定。

      查看链接:
      https://developers.google.com/android-publisher/api_usage
      https://developers.google.com/android-publisher/terms

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-01
        相关资源
        最近更新 更多