簡體   English   中英

如何在Google AdWords API PHP中啟用對客戶數據的脫機訪問

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

Google Adwords API(PHP客戶端)

我試圖讓用戶在我的網站上獲得一次授權,以便能夠獲取其數據用於分析目的。 但是我無法找到一種處理文檔的方法,因為它非常復雜。

我是否需要將它們添加到我的“我的客戶中心”才能執行此操作,或者使用諸如https://developers.google.com/identity/protocols/OAuth2WebServer之類的其他方法

您將必須通過激活了脫機訪問權限的oauth令牌連接用戶,以便請求返回刷新令牌,即使用戶未登錄到您的應用程序,您也可以使用該令牌以編程方式訪問連接:

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

如此處https://developers.google.com/adwords/api/docs/guides/authentication所述,您可以使用來自groogle的oauth游樂場來測試api並查看所需內容。

此外,這是客戶端連接方法的示例(部分為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;
}

然后可以在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();
     }
 }
}

祝好運!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM