0

嗨,我正在使用不同的帐户进行谷歌分析并返回错误:

GDataauthErrorAuthorizationInvalid Credentials

虽然我以前的帐户工作正常。可能是因为我在注册过程中遗漏了一些事情,谁能给我一个详细的注册步骤,以便使用 api_key for google analytics v2.4 为 oauth 2.0 工作

谢谢你

//returns session token for multiple calls to API
function get_session_token($onetimetoken) {
    $output = call_api($onetimetoken, "https://www.google.com/accounts/AuthSubSessionToken");

    if(preg_match("/Token=(.*)/", $output, $matches)) {
        $sessionToken = $matches[1];
    } else {
        echo "Error authenticating with Google.";
        exit;
    }

    return $sessionToken;
}

//gets the data
function call_api($sessionToken,$url){
    $curl = curl_init($url);

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
    if ($_SESSION['authSub']==true){
        $curlheader[0] = sprintf("Authorization: AuthSub token=\"%s\"/n", $sessionToken);
    } else {
        $curlheader[0] = "Authorization: GoogleLogin auth=" . $sessionToken;
    }
    curl_setopt($curl, CURLOPT_HTTPHEADER, $curlheader);

    $response = curl_exec($curl);
    curl_close($curl);

    return $response;       
}

$requrlvisits = sprintf("https://www.googleapis.com/analytics/v2.4/data?ids=ga:%s&dimensions=ga:date&metrics=ga:visits,ga:pageviews,ga:bounces,ga:timeOnSite&start-date=%s&end-date=%s&sort=ga:date&key=%s",$get_profid[9],$date1,$date2,$api_key);
                            // echo $requrlvisits;

                            $visitsxml = call_api($_SESSION['sessionToken'],$requrlvisits);             
                            $visits = parse_data($visitsxml);

                            print_r($visitsxml);
4

0 回答 0