简体   繁体   English

Google Ads API - 如何使用 PHP 获取访问和引用令牌

[英]Google Ads API - How to get the access and referesh token using PHP

I am following this package: https://github.com/googleads/google-ads-php .我正在关注这个 package: https://github.com/googleads/google-ads-php Here they said that I need to run this page:他们在这里说我需要运行这个页面:

https://github.com/googleads/google-ads-php/blob/main/examples/Authentication/GenerateUserCredentials.php https://github.com/googleads/google-ads-php/blob/main/examples/Authentication/GenerateUserCredentials.php

to terminal.到终端。

and I did it like:我这样做是这样的:

php /locaation of the fie/GenerateUserCredentials.php

After that I can get a link which is something like that:之后我可以获得一个类似这样的链接:

https://accounts.google.com/o/oauth2/v2/auth?response_type=code&access_type=offline&client_id=535777736006-d1rp8msevnls2pmihk7b8l23j3vra7duh.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fauth%2Fcallback&state=4e23ce963534701029c1a22d2de7848d034d8b0b0&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fadwords https://accounts.google.com/o/oauth2/v2/auth?response_type=code&access_type=offline&client_id=535777736006-d1rp8msevnls2pmihk7b8l23j3vra7duh.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fauth%2Fcallback&state=4e23ce963534701029c1a22d2de7848d034d8b0b0&scope= https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fadwords

(Above link is demo purpose) (以上链接是演示目的)

So, When I click on this link I redirect to my google account and give access to them and after that I redirect back to this following URL:因此,当我单击此链接时,我会重定向到我的谷歌帐户并授予对它们的访问权限,然后我会重定向回以下 URL:

http://localhost:3000/auth/callback?state=fddbaae6583b15aba552f682fe9f018233388533555f&code=4%2F0AfgeXvs4NV49mrqEBrl81ATRnXhqcu9x9ja8SAbaENSWhmNejGRGkZJE_AcD1aAFWdrlGg&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fadwords http://localhost:3000/auth/callback?state=fddbaae6583b15aba552f682fe9f018233388533555f&code=4%2F0AfgeXvs4NV49mrqEBrl81ATRnXhqcu9x9ja8SAbaENSWhmNejGRGkZJE_AcD1aAFWdrlGg&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fadwords

Here you can see I have 2 params which is state and code .在这里你可以看到我有 2 个参数,分别是statecode

Now my question is how can I get the access token and refresh token using this URL?现在我的问题是如何使用这个 URL 获取访问令牌和刷新令牌?

Here you can see I have 2 params which is state and code .在这里你可以看到我有 2 个参数,分别是statecode

Now you can use them to get tokens.现在您可以使用它们来获取令牌。 Here is an example from the google-ads-php source code (open the link for the full example/code):这是来自 google-ads-php 源代码的示例(打开完整示例/代码的链接):

// Exit if the state is invalid to prevent request forgery.
$state = $request->getQueryParams()['state'];
if (empty($state) || ($state !== $oauth2->getState())) {
    throw new UnexpectedValueException(
        "The state is empty or doesn't match expected one." . PHP_EOL
    );
};

// Set the authorization code and fetch refresh and access tokens.
$code = $request->getQueryParams()['code'];
$oauth2->setCode($code);
$authToken = $oauth2->fetchAuthToken();

$refreshToken = $authToken['refresh_token'];
print 'Your refresh token is: ' . $refreshToken . PHP_EOL;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Ads PHP SDK获取Facebook Marketing API访问令牌 - Get Facebook Marketing API Access Token Using Ads PHP SDK 如何为过期的referesh令牌google api日历编码 - how to code for expired referesh token google api calendar 尝试在Google PHP API中使用Referesh令牌 - Trying to use referesh tokens with google PHP api 如何从访问令牌Google API PHP获取用户电子邮件 - How to get user email from access token google api php 如何使用Google API PHP客户端获取OAuth2访问令牌? - How to get OAuth2 access token with Google API PHP client? 如何使用 php 获取带有谷歌 api 授权码的访问令牌? - How fetch access token with auth code for google api using php? Google Oauth 2.0会检查我的referesh访问令牌是否已过期 - Google Oauth 2.0 check if my referesh access token is expired or not 如何在Google Ads PHP API中通过标签获取帐户? - how to get accounts by label in google ads php api? 如何以编程方式获取访问令牌以使用 PHP 向 Google Merchant Center (Google Shopping) 发送 API 调用? - How to programmatically get the access token to send API call to Google Merchant Center (Google Shopping) with PHP? Google API访问令牌PHP - Google API Access Token PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM