使用Google Play自定义应用API上传APK:账号ID及服务账号获取疑问
Got it, let's break this down step by step to get you past this roadblock with the Google Play Custom App API. Here's exactly what you need to do:
Step 1: Create and Configure Your Service Account
First, you need a service account to authenticate API calls. Here's how to set it up:
- Head to the Google Cloud Console, select your existing project (or create a new one if you don't have one).
- Navigate to IAM & Admin > Service Accounts.
- Click Create Service Account, fill in a name and description, then hit Create and Continue.
- Assign the Play Custom App Publishing role (or the more granular Custom App Publisher role) to this service account, then click Continue > Done.
- Go back to the service accounts list, find the one you just created, click Keys > Add Key > Create New Key. Choose JSON format, download the key file, and store it somewhere secure—you'll need it later.
Step 2: Link the Service Account to Your Play Console
A service account won't work unless it's linked to your Play Developer account:
- Open the Google Play Console with your developer credentials.
- Go to Settings > Users and permissions > Invite new users.
- Enter the email address of your service account (it looks like
[name]@[project-id].iam.gserviceaccount.com). - Assign the Custom App Publisher permission, then send the invite. Wait for the invite to be accepted (it happens automatically for service accounts).
Step 3: Call the Delegate URL to Grab Your Account ID
That URL from the docs is for authorizing the service account to access your Play account and retrieve the account ID:
- Replace
serviceAccountEmailin the URL with your service account's email, andcallbackUrlwith any valid URL (even a local one likehttp://localhost:8080/callbackworks—you just need to extract theaccountIdfrom the redirect). - Open the modified URL in a browser, log in with your Play Developer account, and grant permission. After redirecting, look at the URL in your browser's address bar—you'll see a
accountIdparameter. Copy that value; this is the ID you need for your API calls.
Step 4: Use GoogleCredential for API Authentication
Here's a complete Java code example that ties everything together, using the GoogleCredential you mentioned:
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.gson.GsonFactory; import com.google.api.services.playcustomapp.PlayCustomApp; import com.google.api.services.playcustomapp.model.CustomApp; import java.io.FileInputStream; import java.io.IOException; import java.util.Collections; public class CustomAppUploader { public static void main(String[] args) throws IOException { // Replace these with your own values String serviceAccountKeyPath = "/path/to/your/service-account-key.json"; String playAccountId = "your-copied-account-id"; String apkFilePath = "/path/to/your/app.apk"; // Build the credential using your service account key GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream(serviceAccountKeyPath)) .createScoped(Collections.singleton("https://www.googleapis.com/auth/playcustomapp")); // Initialize the Play Custom App API client PlayCustomApp playCustomApp = new PlayCustomApp.Builder( new NetHttpTransport(), GsonFactory.getDefaultInstance(), credential ).setApplicationName("Your App Name").build(); // Define your custom app details CustomApp customApp = new CustomApp().setTitle("My Custom App"); // Upload the APK and create the custom app playCustomApp.accounts().customApps().create( playAccountId, customApp, new FileContent("application/vnd.android.package-archive", new java.io.File(apkFilePath)) ).execute(); System.out.println("Custom app uploaded successfully!"); } }
Quick Notes to Avoid Headaches
- Make sure you've enabled the Play Custom App API in the Google Cloud Console (search for it in the API Library and toggle it on).
- If you run into permission errors, double-check that your service account has the right roles in both Google Cloud Console and Play Console.
- You don't need a running server for the callback URL—just copy the
accountIdfrom the address bar after the redirect.
内容的提问来源于stack exchange,提问作者desi Joe
相关产品推荐
相关产品推荐

