Google calendar event create and Google business account - php

I am using PHP and Google API for create the google calendar and invite the people. Previously it was worked. But now google change the API. So I also changed the library.
Library
After that I got the permission issue. So I found the solution from google developer account.
Delegate domain-wide authority to your service account
https://developers.google.com/drive/web/delegation
But according to this method I didn't see any Manage third party OAuth Client access
Anybody know any solution or anyway to create the google calendar and invite people?
NOTE: I tried the gmail account. It is working. But when I try the google business account, I have above problem.
Screenshot

In the authentication->click on "Manage API client access". It is same as "Manage third party OAuth Client access" in the documentation.

Related

Correct OAuth2.0 setup for Service Account to use Google Ads Api?

I have created my service account, following the Google documentation as best I can. I created a JSON Key File and have used it successfully to create and refresh my access token, but when I try to call the Google Ads API using that access token I get a 401 with the message "User in the cookie is not a valid Ads user."
I am using a PHP cURL request, not the Google Client library.
My suspicion is that I have something set up incorrectly somewhere between the Master Ad account, the service account and the project in the Google Cloud Console, but I am finding the documentation confusing and unhelpful.
I submitted a question to the Google Ads API google group, and the support person said that my setup looked OK, but also admitted that he cannot see all of it from his end.
I have created the following pieces of the puzzle:
Google Ads Master Account
Developer Token
Project in Google Cloud Console
Service Account in Project
Private Key for Service Account
Set email of Master Ads Account to role of Owner of Service Account
Enabled Domain-Wide Delegation for the Service Account with scope "https://www.googleapis.com/auth/adwords"
Requested and received Access Token with the private key in the JSON file
Please let me know what extra details I should provide to get my issue resolved. Thanks in advance.
My error was that when I created my Access Token, I passed the Gmail account that owns the Google Ads Manager Account to the parameter $sub, but to access Google Ads API the Service Account must impersonate a user in the domain that is registered with the Google Workspace. This point is made in the documentation, but is rather understated.
To fix the issue I granted access to the Google Ads Manager Account to an email account in the Workspace domain, and passed that email address to $sub; now I have made a successful test call to the Google Ads API.
I attach an anonymized summary of my configuration in case it helps anyone who might be reading this in the future.

Unable to read from google analytics

I've followed the google analytics tutorial and created a PHP file with and added service key. But now I get this error:
User does not have any Google Analytics account
For the website where I'm given the access to view the traffic data for, I'm not given access to add new user. But my email(gmail) already added to their google analytics and I have access.
I did research online and came across this: Analytics Google API Error 403: “User does not have any Google Analytics Account”
Here They ask to add the email created upon the service key generation into the google analytics platform. Since I don't have access to add new user I couldn't do this.
What is the correct way for me to read the data?
User does not have any Google Analytics account
Means exactly that. The user you are authenticating does not have access to any google analytics accounts. You need to remember that a service account is not you. A service account is like a dummy user. It has its on google drive account, Google calendar account and probably a bunch more.
Service accounts need to be pre approved this is why you dont get the normal pop up Oauth2 consent screen.
To do this you go to the google analtyics website under the admin section for the account you wish to access using the service account. At the account level take the service account email address and grant it access like you would any other user.
It will then have access to read from your Google analytics account.
Looks like you need access the analytics API but instead, you were just given access to the Google Analytics tool.
For you to use the google analytics api, you need to create a service account and ask your admin to add the service account to GA, just like how they added your account to GA.
detailed documentation

Can not access the Adsense Management API

Can not access the AdSense API no matter what I try. I have tried both service account and OAuth authentications.
What I am trying to accomplish:
Creating a bot that will grab the Adsense revenue earned and store it in an in-house database for tracking revenue over time. I do not want to have authentication issues, I want a server-to-server configuration.
What I do know is you can have access tokens and refresh tokens under OAuth. I've yet to be able to make them work under any available APIs with Adsense. Note: I have built an API to work with AdWords no problem.
Errors: (Between the types of authentication, I can not pass these)
"Account Not Found"
"Client is unauthorized to retrieve access tokens using this method."
What I have done so far:
The Adsense Management API is enabled.
Credentials Created: Service Account
Credentials Created: oAuth 2.0 Client ID
The GSuite account has access to APIs (includes "enabled api access" and "managed API client access")
Libraries (trying) to use:
https://developers.google.com/api-client-library/php/
https://github.com/google/google-api-php-client
https://github.com/google/google-api-php-client-services
(outdated) https://github.com/googleads/googleads-adsense-examples
Example code: (using the service account)
$client = new \Google_Client();
$client->setAuthConfig(WRITEPATH . 'auth/adsense-client.json');
$client->setIncludeGrantedScopes(true);
$client->addScope('https://www.googleapis.com/auth/adsense');
$client->setSubject('email#example.com');
// trying to fire the services
$service = new \Google_Service_AdSense($client);
$report = $service->accounts_reports->generate('clientId', $startDate, $endDate, $optParams);
This script fires the "Client is unauthorized to retrieve access tokens using this method." error message. However, the email address being used is able to access the adsense account, the APIs are enabled and configured on the admin domain level.
Is there an easier way? is there anyone who can lead me in the right direction?
Service account
I am not all that sure that Service accounts work with adsence. You can try by going to the adsence website and under manageUsers add the service account email address. The issue here is its going to sit there at pending i think because the service account needs to confirm that it has been added. There is no way to do that.
Admin domain level
If you are trying to add domain wide delegation to this i wouldn't bother with that either as far as i can see its not supported either.
Client is unauthorized to retrieve access tokens using this method.
As for that error i may be able to help. It means that you have taken the credentials file you downloaded for a service account and are trying to use it with the code for Oauth2. or visa versa. Download the credentials file again and try again.
I have some sample code that may help here

Diffrence b/w Google Work account and Google service account

I am trying to access events from google calendar and I came across on solution
here, This script require google service account, I have one doubt is google service account is free or require Google Work account for my application ?
Discussed in Delegating domain-wide authority to the service account and shown as the best alternative:
If you have a Google Apps domain—if you use Google Apps for Work, for example—an administrator of the Google Apps domain can authorize an application to access user data on behalf of users in the Google Apps domain. For example, an application that uses the Google Calendar API to add events to the calendars of all users in a Google Apps domain would use a service account to access the Google Calendar API on behalf of users. Authorizing a service account to access data on behalf of users in a domain is sometimes referred to as "delegating domain-wide authority" to a service account.
Furthermore,
To delegate domain-wide authority to a service account, first enable domain-wide delegation for an existing service account in the Service accounts section of the Developers Console's Permissions page or create a new service account with domain-wide delegation enabled.
Lastly, setting up of service account is is automatically done upon creating your project while your application runs on Google App Engine.

Getting data from a analytics account using Google Analytics API and PHP

I'm developing a backoffice for a website in PHP.
In that backoffice I would like to output some google analytics info refering to that site. And only that site.
For that, I looked into the google analytics API, but I'm having some problems that I don't know if can overcome.
I don't want the API to ask for the user to login or authorization.
I want the API to access always the same account (mine) and not another google analytics account, so no authorization required.
Can that be done?
Can someone point me to a good tutorial about using the API in the context I'm looking for?
Thank You.
You can't do this with the regular API; as you mentioned, it requires the user to login. However, you can do this with the Google Analytics superProxy API.
Yes - it's possible.
You CAN connect directly to the API without it asking for authorization.
See:
Service Applications and Google Analytics API V3: Server-to-server OAuth2 authentication?

Categories