使用google-api-php-client创建WebToken失败(Android Management API)
Fixing "Invalid parameter: tokenSpec.parent" when creating WebToken via Android Management API (PHP)
Looks like you're hitting two key issues with your WebToken creation code—let's break this down and fix it:
1. You're not using the nested tokenSpec structure correctly
The Android Management API expects parentFrameUrl and permissions to be nested inside a tokenSpec object, not directly on the WebToken itself. Your current code sets these properties directly on the Google_Service_AndroidManagement_WebToken instance, which doesn't map to the API's expected request structure.
2. ParentFrameUrl must use HTTPS
The API requires the parent frame URL to be a secure HTTPS address—your current URL uses HTTP, which will trigger validation errors.
Corrected Code
Here's how to adjust your code to meet both requirements:
// Initialize client (your existing code is fine here) $client = new Google_Client(); $client->useApplicationDefaultCredentials(); $client->addScope("https://www.googleapis.com/auth/androidmanagement"); $client->setApplicationName("MyAppName"); $androidmanagementService = new Google_Service_AndroidManagement($client); // 1. Create a WebTokenSpec object first $webTokenSpec = new Google_Service_AndroidManagement_WebTokenSpec(); $webTokenSpec->setPermissions(array("APPROVE_APPS")); // Switch to HTTPS for your parent frame URL $webTokenSpec->setParentFrameUrl("https://37.71.157.174/mdm/"); // 2. Create the WebToken and attach the tokenSpec $webToken = new Google_Service_AndroidManagement_WebToken(); $webToken->setTokenSpec($webTokenSpec); // 3. Execute the creation request try { $webtoken = $androidmanagementService->enterprises_webTokens->create("enterprises/entname", $webToken); // Success! Use the token value here echo "WebToken created: " . $webtoken->getValue(); } catch (Google_Service_Exception $e) { echo "Error: " . $e->getMessage(); }
Why This Works
- By creating a
WebTokenSpecand assigning it to the WebToken'stokenSpecproperty, you're matching the exact structure the API expects, which resolves thetokenSpec.parentinvalid parameter error. - Switching to HTTPS ensures your parent frame URL passes the API's security validation checks.
If you still run into issues, double-check:
- That your enterprise ID (
enterprises/entname) is correct and matches the one used in your working policy updates. - That your server's HTTPS setup is valid (self-signed certificates might work for testing but aren't recommended for production).
内容的提问来源于stack exchange,提问作者christophe
相关产品推荐
相关产品推荐

