googleapis / google-api-go-client

Auto-generated Google APIs for Go.
https://pkg.go.dev/google.golang.org/api
BSD 3-Clause "New" or "Revised" License
4.04k stars 1.13k forks source link

Unable to publish custom app throwing Error 401: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential #2281

Closed srinuu7878 closed 12 months ago

srinuu7878 commented 12 months ago

my PHP code <?php ini_set('display_errors', '1'); ini_set('display_startup_errors', '1'); error_reporting(E_ALL); require DIR . '/vendor/autoload.php'; // Path to the Google API PHP client library session_start();

    $devAccountId = '6387507453241171720';
    $apkPath = '/opt/bitnami/apache/htdocs/mdm.apk';
    $clientSecretsJson = '/opt/bitnami/apache/htdocs/createapp/Auth_2_credintials.json';
    $scope = 'https://www.googleapis.com/auth/androidpublisher';

    $client = new Google_Client();
    $client->setAuthConfig($clientSecretsJson);
    $client->addScope($scope);

if (isset($_GET['code'])) { $token = $client->fetchAccessTokenWithAuthCode($_GET['code']);

   $client->getAccessToken($token);
   $accessToken = $token['access_token'];
       print_r($accessToken);
        //print_r($_GET['access_token']);
        //print_r("acccess token generated");

    //$client->setAuthConfig($clientSecretsJson);

   //$credential = $client->fetchAccessTokenWithAssertion();

    //print_r($credential);

    //$client->setAccessToken($credential);   
    //$gauth = new Google_Service_Oauth2($client);

/* $app = new Google_Service_Playcustomapp($client);

   $customApps = $app->accounts_customApps;
   print_r("Custom accountsstart");
   print_r($customApps);
   print_r("Custom accountsstart");
    $customApp = new Google_Service_Playcustomapp_CustomApp();
    $customApp->setTitle('VR Doctors Academy');
    $customApp->setLanguageCode('en_US');
    $customApp->setOrganizations([
                                  "organizationId"=> "LC04bt87ss",
                                  "organizationName"=> "Prabhak Client-3"
                                ]);
    $customApp->setPackageName('com.prabhaktech.app'); */

    #$resp = $customApps->create($devAccountId,$customApp,array("data"=> file_get_contents($apkPath)));

// Read APK file content $apkContent = file_get_contents($apkPath);

$appMetadata = [ 'title' => 'VR doctors', 'languageCode' => 'en_US', 'organizations' => [ [ 'organizationId' => 'C0123wxyz', 'organizationName' => 'My organization' ] ] ];

// Create metadata-only request data $metadataRequestData = [ 'custom_app' => $appMetadata, ];

// Set up cURL options for metadata-only request $metadataCurl = curl_init();

curl_setopt_array($metadataCurl, [ CURLOPT_URL => 'https://playcustomapp.googleapis.com/playcustomapp/v1/accounts/' . $devAccountId . '/customApps', CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode(['appMetadata' => $appMetadata]), CURLOPT_HTTPHEADER => [ 'Content-Type: application/json', 'Authorization: Bearer ' . $accessToken, ], ]);

// Execute metadata-only cURL request $metadataResponse = curl_exec($metadataCurl);

// Close metadata-only cURL session curl_close($metadataCurl);

// Decode the metadata response $metadata = json_decode($metadataResponse, true);

print_r($metadata);

// Check if metadata request was successful if (!isset($metadata['custom_app'])) { echo "Metadata request failed: " . $accessToken; exit; }

// Media upload request data $apkContent = file_get_contents($apkPath);

// Set up cURL options for media upload request $uploadCurl = curl_init();

curl_setopt_array($uploadCurl, [ CURLOPT_URL => 'https://playcustomapp.googleapis.com/upload/playcustomapp/v1/accounts/' . $devAccountId . '/customApps?uploadType=multipart', CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => [ 'metadata' => json_encode($metadata['custom_app']), 'file' => base64_encode($apkContent), // Base64 encode the file content ], CURLOPT_HTTPHEADER => [ 'Content-Type: multipart/form-data', 'Authorization: Bearer ' . $accessToken, ], ]);

// Execute media upload cURL request $uploadResponse = curl_exec($uploadCurl);

// Close media upload cURL session curl_close($uploadCurl);

// Print the upload response print_r($uploadResponse); } else { if (isset($_GET['access_token']) && $_GET['access_token']) { $client->setAccessToken($_SESSION['access_token']); } else { echo ("access_token token not availanle \n");

    header('Location: ' . filter_var('https://accounts.google.com/o/oauth2/v2/auth?response_type=code&access_type=online&client_id=669680446239-hvb767pgheo3rjubv5h7vcb5ufput0kg.apps.googleusercontent.com&redirect_uri=https%3A%2F%2Fmdm.prabhaktech.com%2Fcreateapp%2Fcustomapp.php&state&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fandroidpublisher&approval_prompt=auto', FILTER_SANITIZE_URL));
           }

}

?>

srinuu7878 commented 12 months ago

its very urgent issue to be fixed can some one help us where is the actual problem,

codyoss commented 12 months ago

This library is for Go. If you have a PHP issue maybe try https://github.com/googleapis/google-auth-library-php