Zeone line
Zeone line

Reputation: 339

Acknowledges a subscription purchase, Request is missing required authentication credential. Expected OAuth 2 access token

Hi im strugling to Acknowledges a subscription purchase via server side using PHP i dont want to use or install any package, even i created the API key i get a response: Request is missing required authentication credential. Expected OAuth 2 access token

WHERE DO I GET TOKEN FROM AND HOW TO? ISNT API KEY ENOUGHT?

i fellowed this documentation from google Acknowledges a subscription purchase.

              $data = [];
            $packageName = $request->package_name; // Replace with your app's package name
            $token = $request->purchase_token; // Replace with the subscription token

            try {
                
                // Replace with your actual values
                $url = "https://androidpublisher.googleapis.com/androidpublisher/v3/applications/$packageName/purchases/subscriptionsv2/tokens/$token";

                // Set up cURL options
                $ch = curl_init($url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Warning: This is for demonstration purposes only, verify certificates in production

                // Set the Authorization header
                $apiKey = '38ee606c94233649f666666666666666666'; // Replace with your actual API key stored securely in .env file
                $authorizationHeader = "Authorization: Bearer " . $apiKey ;
                curl_setopt($ch, CURLOPT_HTTPHEADER, [$authorizationHeader]);

                // Execute the request
                $response = curl_exec($ch);

                // Check for errors
                if (curl_errno($ch)) {
                    $error = curl_error($ch);
                    // echo "Error: $error";
                    $data = $error;
                    
                   return new ProductResource(['status' => '1', 'message' => 'Purchases subscriptions acknowledge curl_errno', 'result' => $resultSave, 'data' =>  $response ]);
                    
                } else {
                    // Process the response
                    $data = json_decode($response, true);
                    // Process the response data here
                   // echo "Subscription details: " . json_encode($data, JSON_PRETTY_PRINT);
                }

                if ($resultSave) {
                    return new ProductResource(['status' => '1', 'message' => 'Purchases subscription saved in database and acknowledge successfully', 'result' => $resultSave, 'data' =>  $response ]);
                } else {
                    return new ProductResource(['status' => '1', 'message' => 'Purchases subscriptions not saved in database and acknowledge successfully', 'result' => $resultSave, 'data' =>  $response ]);
                }


            } catch (Throwable $e) {
               // echo "Exception: " . $e->getMessage();
                $data = $e->getMessage();

                return new ProductResource(['status' => '0', 'message' => 'Failed to acknowledge subscriptions purchases catch (Throwable) ', 'result' => $resultSave, 'data' =>  $data]);

            }

when running the code above i get this error

    {
  "error": {
    "code": 401,
    "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "errors": [
      {
        "message": "Login Required.",
        "domain": "global",
        "reason": "required",
        "location": "Authorization",
        "locationType": "header"
      }
    ],
    "status": "UNAUTHENTICATED",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.ErrorInfo",
        "reason": "CREDENTIALS_MISSING",
        "domain": "googleapis.com",
        "metadata": {
          "service": "androidpublisher.googleapis.com",
          "method": "androidpublisher.SubscriptionPurchasesV2Service.Get"
        }
      }
    ]
  }
}

Upvotes: 0

Views: 146

Answers (0)

Related Questions