简体   繁体   English

从 Android 应用程序使用 Android Google Play Dev API

[英]Using Android Google Play Dev API from Android app

I would like to implement the Google Dev API within an android app.我想在 android 应用程序中实现 Google Dev API。 I am trying to use the samples provided by Google for Java, here: https://github.com/googlesamples/android-play-publisher-api/tree/master/v3/java/src/com/google/play/developerapi/samples我正在尝试使用谷歌为 Java 提供的示例,这里: https : //github.com/googlesamples/android-play-publisher-api/tree/master/v3/java/src/com/google/play/developerapi /样品

I just modified them to provide the InputStream of the client-secret.json file using the AssetManager of Android.我只是将它们修改为使用 Android 的 AssetManager 提供 client-secret.json 文件的 InputStream。

Yet, this is still not really compatible with Android, because of java.awt dependencies as it looks like in the error below:然而,这仍然不是真正与 Android 兼容,因为它在下面的错误中看起来像 java.awt 依赖项:

  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)

For the record, the code around line 140 is :作为记录,第 140 行附近的代码是:

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

which involves the dependencies:其中涉及依赖项:

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

How can I convert this samples to make them compatible with Android.如何转换此示例以使其与 Android 兼容。 I have no clue how they work so I am just copy/pasting this in an AsyncTask that is supposed to establish the connection and list my dev apks.我不知道它们是如何工作的,所以我只是将它复制/粘贴到一个 AsyncTask 中,它应该建立连接并列出我的开发 apk。

You must logging in through the play-services-auth library.您必须通过 play-services-auth 库登录。

This code sample may help you:此代码示例可以帮助您:

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
    //.....
}

WARNING!警告!

I suppose that publishing your application containing this library violates Google Play policy through the following scenarios (described by links below):我认为发布包含此库的应用程序会通过以下场景(由以下链接描述)违反 Google Play 政策:

Do not allow third parties to use the APIs to publish to your account on your behalf.不允许第三方使用 API 代表您发布到您的帐户。 Do not use the APIs to offer a developer service or tool which creates, uploads, publishes, distributes or updates apps to the Google Play Store.请勿使用 API 提供开发者服务或工具,以在 Google Play 商店中创建、上传、发布、分发或更新应用程序。 Doing this is a violation of the Google Play Developer API Terms of Service.这样做违反了 Google Play Developer API 服务条款。 Such actions may result in your account being suspended, and your apps removed from the Google Play Store.此类操作可能会导致您的帐户被暂停,并且您的应用会从 Google Play 商店中删除。 Everything done on your account is your responsibility, and allowing a third party to use your account puts all of your users at risk.在您的帐户上所做的一切都是您的责任,允许第三方使用您的帐户会使您的所有用户面临风险。

and terms:和条款:

Publishing API: You will only use the Publishing API for your own activities related to your distribution of products under the Google Play Developer Distribution Agreement. Publishing API:您将仅将 Publishing API 用于与您根据 Google Play 开发者分发协议分发产品相关的活动。 You will not utilize a third party's Developer Account to access the Publishing API, use the Publishing API to publish apps on behalf of a third party through a developer tool or service, or share your own Developer Account to allow a third party to access the Publishing API on your behalf.您不会利用第三方的开发者账号访问发布 API,使用发布 API 通过开发者工具或服务代表第三方发布应用,或共享您自己的开发者账号以允许第三方访问发布代表您的 API。 A violation of this provision may result in suspension, termination of access to the Publishing API, your Developer Account, and/or suspension or termination of third-party apps created, uploaded, published, distributed or updated in violation of this provision.违反本规定可能会导致暂停、终止对发布 API、您的开发者帐户的访问,和/或暂停或终止违反本规定创建、上传、发布、分发或更新的第三方应用程序。

See links:见链接:
https://developers.google.com/android-publisher/api_usage https://developers.google.com/android-publisher/api_usage
https://developers.google.com/android-publisher/terms https://developers.google.com/android-publisher/terms

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM