I am trying to connect to google merchant with php curl. I keep getting a error that says status": "UNAUTHENTICATED" I can connect fine on the api https://developers.google.com/shopping-content/reference/rest/v2/products/ and run examples from there. I can also runs tests from https://developers.google.com/oauthplayground/ I found this but I keep getting unauthenticated error $merchantid = xxxxxxxxx; //this is a 9 digit code found in top left of Merchant Center $email = 'xxxxxxxxxxx@merchant-center-xxxxxxxxxxxx.iam.gserviceaccount.com'; //this is the email address assigned to the service account $privatekey = "-----BEGIN PRIVATE KEY-----\nXXXXXapprox 6 lines long key from json fileXXXXXX\n-----END PRIVATE KEY-----\n"; //this is the long key that you download within the json file at the end of the service account setup $header = json_encode(['alg' => 'RS256', 'typ' => 'JWT']); $base64UrlHeader = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($header)); $iat = strtotime("now"); $exp = strtotime("+1 hour"); $currenttime = date("H:i:s"); $claimset = json_encode(['iss' => $email, 'scope' => 'https://www.googleapis.com/auth/content', 'aud' => 'https://www.googleapis.com/oauth2/v4/token', 'exp' => $exp, 'iat' => $iat]); $base64UrlClaimSet = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($claimset)); $binary_signature = ""; $algo = "SHA256"; openssl_sign($base64UrlHeader.".".$base64UrlClaimSet, $binary_signature, $privatekey, $algo); $jwtSignature = str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($binary_signature)); $jwt = $base64UrlHeader . "." . $base64UrlClaimSet . "." . $jwtSignature; $url = "https://www.googleapis.com/oauth2/v4/token"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=".$jwt); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $data = curl_exec($ch); $accesstoken = substr($data, strpos($data, 'access_token') + 16); $arr = explode('"',trim($accesstoken)); $accesstoken = $arr[0]; $url = "https://shoppingcontent.googleapis.com/content/v2/".$merchantid."/products"; $header = array( 'Authorization: Bearer '.$accesstoken ); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $data = curl_exec($ch); echo "<br/><br/>data: ".$data; PHP: any suggestions or pointers in the right direction would greatly be appreciated.
other notes - if i use oauthplayground and get access token i can copy directly into the file below and it will work fine. Problem is that token expires after 3000 seconds. I think but am not sure there is either a permanent access token that doesn't expire or I can somehow use the api credentials I assume. I need to permanent solution built in or something where I only have to add the new token every year kind of like ebays api settings. I don't think this violating anything. But I do have this job posted on another site if anyone is interested
I have not worked with google merchant API specifically, but have worked with other similar APIs. I think its failing because you first need to send a different request to google, which will return an authorization token for next request. This is done in background. Then that token needs to be sent to google along with client redirect, to make the payment. I think you are missing the background step which is why you are getting authorization error because of missing token.
yup thats right. Soo you have your first request which grabs the token, then after that you use a timed token which is what I was doing. Then copy and pasting. My goal is to have this setup on the back end to automatically download orders. I have worked with other apis too but not google. So what I am trying to accomplish is some sort of google api where I can directly download the orders which I can't seem to find so far (think I am just looking for the wrong stuff but do not know where to look) or something like this where the system automatically grabs a new timed token everytime orders need to be downloaded into the system. sooo Option A - auto grab the short timed token - 4000 seconds Option B - use the api like another api like ebay paypal amazon that lasts a year or two expiration. I am new to this google api stuff I mean I have done stuff with maps and other stuff like that but all that other stuff is written a lot more easier plus theres a lot of simple examples I can look at and understand much more quickly.
Does anyone know where to find more google api info? or a site thats more depth about this stuff? or maybe a number or chat to contact?
I ended up just installing composer virtual server and the google code library , and downloaded everything else that way. Then copied the files over to the server itself. For refrence if anyone is interested - github.com/googleads/googleads-shopping-samples/tree/master/php and https://github.com/googleads/googleads-shopping-samples/tree/master/php and as for apis - console.cloud.google.com/apis/credentials/key/ and console.cloud.google.com/apis/credentials?project=NAME OF YOUR PROJECT. Seems to work pretty well. I understand the concept of composer and its a lovely thought, but still today a lot of companies web hosting places do not allow those permissions wish they had two versions would be less of a hassle that way you could use it without composer and without downloading the google code library.