Google API p12键无法读取:错误:0D0680A8:asn1编码例程:ASN1_CHECK_TLEN:标记错误

时间:2014-12-15 12:00:07

标签: php google-api google-analytics-api google-api-php-client service-accounts

任何人发现这个 - 这个错误是由于在win7盒子上下载后.p12被破坏了。下载到unix机器后证书工作。

我已经跟踪了大约一百万个不同的指南,试图让它发挥作用。

我正在尝试使用Google Analytics API连接到service account

我有相关的"项目"在控制台中创建,因为项目访问了相关API所需的。

在凭据中,我在OAuth部分添加了服务帐户,下载了p12密钥并存储在服务器上。

当我运行代码时:

//start the google v3 api server authorization with the .p12 key
    $client = new \Google_Client();
    $client->setApplicationName("AnalyticsAPI");

    $key = __DIR__ . '/google-keys/AnalyticsAPI-XXXXXX.p12';

    $credentials = new \Google_Auth_AssertionCredentials(
        '101XXXXXXXXXXXXXXXXXXXXXnq4omne@developer.gserviceaccount.com',
        array('https://www.googleapis.com/auth/analytics.readonly'),
        $key
    );
    $client->setAssertionCredentials($credentials);
    //auto refresh if old
    if($client->getAuth()->isAccessTokenExpired()) {
        $client->getAuth()->refreshTokenWithAssertion($credentials);
    }

    //start the analytics shtuff
    $service = new \Google_Service_Analytics($client);  
    $accounts = $service->management_accountSummaries->listManagementAccountSummaries();

    //Adding Dimensions
    $params = array('dimensions' => 'ga:pagePath');
    // requesting the data
    $data = $service->data_ga->get("ga:$profile_id", $start_date,  $end_date, "ga:users,ga:sessions", $params );

    print_r($data);

第52行"" Google / Signer / P12.php引发错误

Unable to parse the p12 file. Is this a .p12 file? Is the password correct? OpenSSL error: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag in /XXXXX/classes/google-api-php-client-master/src/Google/Signer/P12.php on line 52

错误是从... Signer / P12.php @ 49:

引发的
// This throws on error
      $certs = array();
      if (!openssl_pkcs12_read($p12, $certs, $password)) {
        throw new Google_Auth_Exception(
            "Unable to parse the p12 file.  " .
            "Is this a .p12 file?  Is the password correct?  OpenSSL error: " .
            openssl_error_string()
        );
      }

当我提取试图读取.p12文件并自行运行的相关代码时,我得到同样的错误:

$certs = array();
    openssl_pkcs12_read($key, $certs, 'notasecret');
    print_r($certs);
    echo openssl_error_string();
    die(x);


Array ( ) error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag

我完全难过了。阅读关于这个问题的其他一些帖子: Getting "Unable to parse the p12 file..." Error With google-api-php-client

我试过了

  1. 确保权限正确。
  2. file_get_contents($key)然后转到openssl_pkcs12_read,产生相同的结果!
  3. 有没有人有任何线索?

1 个答案:

答案 0 :(得分:1)

  1. 确保您拥有来自github
  2. 的最新clint库
  3. 授予服务帐户电子邮件地址访问权限以 ACCOUNT 级别阅读Google Analytics
  4. 试用本教程Google Service Account with PHP

    session_start();        
    require_once 'Google/Client.php';
    
    require_once 'Google/Service/Analytics.php';    
    
    /************************************************   
    The following 3 values an befound in the setting    
    for the application you created on Google       
    Developers console.      Developers console.
    The Key file should be placed in a location  
    that is not accessable from the web. outside of 
    web root.        web root.
    
    In order to access your GA account you must 
    Add the Email address as a user at the  
    ACCOUNT Level in the GA admin.      
    ************************************************/
    
    $client_id = '1046123799103-nk421gjc2v8mlr2qnmmqaak04ntb1dbp.apps.googleusercontent.com';
    $Email_address = '1046123799103-nk421gjc2v8mlr2qnmmqaak04ntb1dbp@developer.gserviceaccount.com';     
    $key_file_location = '629751513db09cd21a941399389f33e5abd633c9-privatekey.p12';     
    $client = new Google_Client();      
    $client->setApplicationName("Client_Library_Examples");
    
    $key = file_get_contents($key_file_location);   
    
    // seproate additional scopes with a comma   
    $scopes ="https://www.googleapis.com/auth/analytics.readonly";  
    
    $cred = new Google_Auth_AssertionCredentials(
    
        $Email_address,      
    array($scopes),     
    $key         
    );      
    $client->setAssertionCredentials($cred);
    if($client->getAuth()->isAccessTokenExpired()) {        
            $client->getAuth()->refreshTokenWithAssertion($cred);       
    }       
    $service = new Google_Service_Analytics($client);
    $accounts = $service->management_accountSummaries->listManagementAccountSummaries();
    
    //calulating start date  
    $date = new DateTime(date("Y-m-d"));     
    $date->sub(new DateInterval('P10D'));    
    //Adding Dimensions
    $params = array('dimensions' => 'ga:userType'); 
    // requesting the data  
    $data = $service->data_ga->get("ga:78110423", $date->format('Y-m-d'), date("Y-m-d"), "ga:users,ga:sessions", $params );  
    ?><html>     
    <?php echo $date->format('Y-m-d') . " - ".date("Y-m-d"). "\n";?>    
    <table>  
    <tr>     
    <?php    
    //Printing column headers
    foreach($data->getColumnHeaders() as $header){
            print "<td>".$header['name']."</td>";       
    }       
    ?>      
    </tr>       
    <?php       
    //printing each row.
    foreach ($data->getRows() as $row) {        
            print "<tr><td>".$row[0]."</td><td>".$row[1]."</td><td>".$row[2]."</td></tr>";   
    }    
    //printing the total number of rows
    ?>      
    <tr><td colspan="2">Rows Returned <?php print $data->getTotalResults();?> </td></tr>     
    </table>     
    </html>     
    
  5. 代码从前面提到的教程中删除。