简体   繁体   English

如何在Google AdWords API PHP中启用对客户数据的脱机访问

[英]How to enable offline access to customer data in google adwords api php

Google Adwords API (PHP Client) Google Adwords API(PHP客户端)

I am trying to get a user to authorize once on my website to be able to get his data for analytical purposes. 我试图让用户在我的网站上获得一次授权,以便能够获取其数据用于分析目的。 But I can not figure out a way to do it with the documentation it is quite complex. 但是我无法找到一种处理文档的方法,因为它非常复杂。

Do I need to add them to my mcc to be able to do this or is there another way using something like https://developers.google.com/identity/protocols/OAuth2WebServer 我是否需要将它们添加到我的“我的客户中心”才能执行此操作,或者使用诸如https://developers.google.com/identity/protocols/OAuth2WebServer之类的其他方法

You will have to connect the user via oauth token with offline access activated, so that the request returns a refresh token, that you can use to programmatically access the connection, even if the user is not logged into you application: 您将必须通过激活了脱机访问权限的oauth令牌连接用户,以便请求返回刷新令牌,即使用户未登录到您的应用程序,您也可以使用该令牌以编程方式访问连接:

$client->setApprovalPrompt('force');
$client->setAccessType('offline'); 

As explained here https://developers.google.com/adwords/api/docs/guides/authentication you can use the oauth playground from groogle to test the api and see what you need. 如此处https://developers.google.com/adwords/api/docs/guides/authentication所述,您可以使用来自groogle的oauth游乐场来测试api并查看所需内容。

Additionally here is an example for the client connection method (partially Laravel specific code, but should be good enough to explain the procedure): 此外,这是客户端连接方法的示例(部分为Laravel特定代码,但足以解释该过程):

function googleIntegrationClient($refresh=false)
{
    $client = new \Google_Client();
    $client->setClientId(env('GOOGLE_OAUTH_CLIENT_ID'));
    $client->setClientSecret(env('GOOGLE_OAUTH_CLIENT_SECRET'));
    $client->setRedirectUri(env('GOOGLE_OAUTH_REDIRECT_URI'));
    $client->setApplicationName('App Google Integration');
    $client->addScope("profile");
    $client->addScope("email");
    $client->addScope("https://www.googleapis.com/auth/adwords");

    //make sure there is a refresh token in the request for offline access.
    $client->setApprovalPrompt('force');
    $client->setAccessType('offline');

    if($refresh)
    {
        //get currently logged in user
        $user = \Auth::user();
        //set token from user data
        $client->setAccessToken($user->settings["integration"]["google"]['token_data']);

        //check if token is valid
        if($client->isAccessTokenExpired())
        {
            //as token is invalid set refresh token
            $token_data = json_decode($user->settings["integration"]["google"]['token_data']);
            $client->refreshToken($token_data->refresh_token);

            //save new token data
            $modify_settings = $user->settings;
            $modify_settings["integration"]["google"]["token_data"] = $client->getAccessToken();
            $user->settings = $modify_settings;
            $user->save();
        }
    }

    return $client;
}

You can use this method then in your oauth connecting routine: 然后可以在oauth连接例程中使用此方法:

//route for redirecting to google oauth
public function redirectToProvider()
{
    $user = \Auth::User();
    $client = googleIntegrationClient();
    $auth_url = $client->createAuthUrl();
    return \Redirect::to(filter_var($auth_url, FILTER_SANITIZE_URL));
}

//callback route to handle the provided data from google oauth
public function handleProviderCallback(Request $request)
{
 $user = \Auth::User();
 $client = googleIntegrationClient();
 $data = $request->all();
 if (isset($data['code']))
 {
     try {
         $client->authenticate($data['code']);
         $token = $client->getAccessToken();
     } catch (Exception $e) {
         $user->settings = array(
             "integration" => array(
                 "google" => array(
                     'active' => false,
         )));
         $user->save();
     }

     if($token)
     {
         $google_oauth = new \Google_Service_Oauth2($client);

         $user->settings = array(
             "integration" => array(
                 "google" => array(
                     'active' => true,
                     'token_data' => $token,
                     'id' => $google_oauth->userinfo->get()->id,
                     'avatar' => $google_oauth->userinfo->get()->picture,
                     'email' => $google_oauth->userinfo->get()->email,
                     'name' => $google_oauth->userinfo->get()->name,
         )));
         $user->save();
     }
 }
}

Good luck! 祝好运!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM