invalid api keys permission denied error in google api using php - php

I am using google api first time my code execute perfectly even give me data using people service api...but second time it give me following error
Type: Google_Service_Exception
Message: { "error": { "code": 403, "message": "The request is missing
a valid API key.", "errors": [ { "message": "The request is missing a
valid API key.", "domain": "global", "reason": "forbidden" } ],
"status": "PERMISSION_DENIED" } }
i am using codeignitor and this is redirected uri controller function...thanks in advance and sorry for poor english..
$client = $this->google_client_setup();
$client->authenticate($this->input->get('code'));
$access_token = $client->getAccessToken();
$people_service = new Google_Service_PeopleService($client);
$profile = $people_service->people->get(
'people/me', array('personFields' => 'names,emailAddresses'));
$access_token = $access_token['access_token'];
$email = $profile['emailAddresses'][0]['value'];
$name = $profile['names'][0]['displayName'];
$user = [
'email' => $email,
'name' => $name,
'is_admin' => 0
];

It's hard to tell, but I would think that if it says it's missing valid api key, you should treat that as answer ;)
Are you being redirected somewhere after first run? That could explain problem Javier commented on.
I'd suggest to dump two things to debug the code:
$this->input->get('code')
and if this is ok, then
$access_token
this should give you idea what your problem is :)
Good luck!

Related

I am getting errors while adding google job posting (indexing api)

I would be really happy to get a solution for this.
This is the error:
{ "error": { "code": 403, "message": "Permission denied. Failed to
verify the URL ownership.", "status": "PERMISSION_DENIED" } }
As I have followed all the steps given in the google documentation.
I have verified the url ownership and added service account as owner. Here is the code snippet. Please check and reply with a solution, if you have.
include_once dirname(__FILE__)."/google-api-php-client_1/vendor/autoload.php";
$client = new Google_Client();
$client->setAuthConfig(dirname(__FILE__).'/xxxxxxxxxxxxxxxxxxxxx.json');
$client->addScope('https://www.googleapis.com/auth/indexing');
$httpClient = $client->authorize();
$endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish';
$array= array("url"=>"https://www.mycoders.in/career","type"=>"URL_UPDATED");
$content= json_encode($array);
$response = $httpClient->post($endpoint, [ 'body' => $content ]);
$response2 = $response->getBody()->getContents();
Thanks in advance!
Rohit
Probably your service account is set to "Full" instead of Owner. If that is the case, you have to change your account to Owner. Use the Old Webmasters tool to do that.
https://support.google.com/webmasters/thread/4763732?hl=en

PHP Google_Service_Dataflow not finding file in bucket

I've come across an issue using the googleapis/google-api-php-client library, specifically the Dataflow Service that I cannot solve.
When I try to use the library I set up the request like so:
$this->client = new \Google_Client();
$this->client->setAuthConfig(config_path('google-service-account.json'));
$this->client->setIncludeGrantedScopes(true);
$this->client->addScope(\Google_Service_Dataflow::CLOUD_PLATFORM);
$body = [
"gcsPath" => "gs://{$this->bucket}/{$this->template}",
"location" => "us-central1",
];
$parameters = new \Google_Service_Dataflow_LaunchTemplateParameters;
$parameters->setJobName($this->jobname);
$parameters->setParameters($body);
$service = new \Google_Service_Dataflow($this->client);
$request = $service->projects_templates->launch($this->project, $parameters);
And I get the following error:
{
"error": {
"code": 400,
"message": "(11f8b78933fc59c3): Bad file name: , expected
'gs://\u003cbucket\u003e/\u003cpath\u003e'",
"errors": [
{
"message": "(11f8b78933fc59c3): Bad file name: , expected
'gs://\u003cbucket\u003e/\u003cpath\u003e'",
"domain": "global",
"reason": "badRequest"
}
],
"status": "INVALID_ARGUMENT"
}
}
It seems that the path is getting corrupted along the way, I've checked and it gets fine until the Guzzle object is instantiated to send the request inside the library.
I'm pretty lost at this point so any suggestion or clue is welcome.
Thank you in advance.
No gcsPath is given in the query params for the request constructed by the SDK.
This is because gcsPath is set to be an option for Google_Service_Dataflow_LaunchTemplateParameters.
It is documented that request query parameters be given as optional params
(See https://github.com/googleapis/google-api-php-client-services/blob/v0.81/src/Google/Service/Dataflow/Resource/ProjectsTemplates.php#L73.)
$opt_params = [
"gcsPath" => "gs://{$this->bucket}/{$this->template}",
"location" => "us-central1",
];
$template_params = [
// Keep template params here.
];
$launch_params = new \Google_Service_Dataflow_LaunchTemplateParameters;
$launch_params->setJobName($this->jobname);
$parameters->setParameters($template_params);
$service = new \Google_Service_Dataflow($this->client);
$request = $service->projects_templates->launch($this->project, $parameters, $opt_params);

PHP - How to update/delete contact (google People) With PHP

for 3 days i'm frustated because google not have tutorial for php.
(sorry for my bad english)
i got an error while updating names :
$client=client_google();
$google_id="people/c3062123412341234";
if ($client->getAccessToken() == "") return null;
$people_service = new Google_Service_PeopleService($client);
$person = new Google_Service_PeopleService_Person();
if($tipenya == "Cancel"){
$name = new Google_Service_PeopleService_Name();
$name->SetFamilyName("Keluarga Cemara");
$name->SetGivenName("Tampan");
$person->setNames($name);
$profile = $people_service->people->get(
$google_id,
array('personFields' => 'metadata'));
$etag = $profile->etag;
$person->setEtag($etag);
$person->setResourceName($google_id);
if($google_id !=''){
//$people_service->people->updatePersonField("names");
$people_service->people->updateContact($google_id,$person);
}
}else if($tipenya=="Delete"){
if($google_id !=''){
$person->setResourceName($google_id);
$people_service->people->deleteContact($person);
}
}
Error when i execute:
exception 'Google_Service_Exception' with message '{
"error": {
"code": 400,
"message": "updatePersonFields mask is required. Please specify one or more valid paths. Valid paths are documented at https://developers.google.com/people/api/rest/v1/people/update.",
"errors": [
{
"message": "updatePersonFields mask is required. Please specify one or more valid paths. Valid paths are documented at https://developers.google.com/people/api/rest/v1/people/update.",
"domain": "global",
"reason": "badRequest"
}
],
"status": "INVALID_ARGUMENT" }}'
You need to specify the parameter updatePersonFields. Seems like the parameters are passed as the last argument by looking at the read examples in the documentation. It probably should look something like:
$params = array('updatePersonFields' => 'names,emailAddresses');
$people_service->people->updateContact($google_id,$person,$params);
I haven't tested the above, so that might not be the exact syntax.

Insufficient Permission

befor i ask i want to introduce my first steps.
I used this for example: PHP Quickstart
Install google/apiclient via composer
Create a project in Google Developers Console
Add credentials to my project
Upload client_secret.json to my webserver
Run the script & enter the code
Credentials saved to /files/php-yt-oauth2.json
This channel's ID is UC_x5XG1OV2P6uZZ5FSM9Ttw. Its title is Google Developers, and it has 125874726 views.
Now i want to list all videos of one playlist:
function playlistItemsListByPlaylistId($service, $part, $params) {
$params = array_filter($params);
$response = $service->playlistItems->listPlaylistItems(
$part,
$params
);
print_r($response);
}
playlistItemsListByPlaylistId($service,
'snippet',
array('maxResults' => 1, 'playlistId' => 'ID'));
An get the following error:
[23-Dec-2017 14:29:59 Europe/Berlin] PHP Fatal error: Uncaught Google_Service_Exception: {
"error": {
"errors": [
{
"domain": "global",
"reason": "insufficientPermissions",
"message": "Insufficient Permission"
}
],
"code": 403,
"message": "Insufficient Permission"
}
}
in /html/sa/vendor/google/apiclient/src/Google/Http/REST.php:118
I dont know whats going wrong and i hope someone can help me with this issue.

YouTube API v3 - keyInvalid Error 400 - PHP

I am always getting this error if I try to get all videos of a channel.
{ "error": { "errors": [ { "domain": "usageLimits", "reason": "keyInvalid", "message": "Bad Request" } ], "code": 400, "message": "Bad Request" } }
My code is the following
$api_key = "AIzaSyD...nR8";
stream_context_set_default(['http' => ['ignore_errors' => true]]);
$source_videos = file_get_contents("https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId={UCuB3qjWes1E8t4yUmbP360Q}&maxResults=1&key={".$api_key."}");
echo $api_key;
echo $source_videos;
I created a project on https://console.developers.google.com/apis/dashboard and added, activated the "YouTube Data API v3", created credentials with "Webserver(eg. Node.js, Tomcat)" as platform and finally copied the key in my php document. I don't know why I can't get it to work. I appreciate help. :)
EDIT:
use Webbrowser(JavaScript) as platform and it should work.
try this code
$API_key = 'AIzaSyCTPwxSs1id2kdv_fupUfbYkfa6Fucp_6A';
$channelID = 'UCTCU28hEulL2jkafQ21uuWA';
$maxResults = 10;
$videoList = json_decode(file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelID.'&maxResults='.$maxResults.'&key='.$API_key.''));
note: you need a virtual server to run php (wamp/mamp/xampp)
Use Webbrowser(JavaScript) as platform and it should work.

Categories