uploadData Google 分析中的文件名

File name in uploadData Google Analytics

我需要使用 API 将 CSV 文件上传到 Google Analytics。一切正常,但列中的文件名显示 "Unknown filename".



这是我的代码:

$analytics = new \Google_Service_Analytics($client);
try {
    $analytics->management_uploads->uploadData(
        '<acc id>',
        'UA-xxx',
        'xxxxxxxxxxxxxx',
        array(
            'data' => file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/ex.csv'),
            'mimeType' => 'application/octet-stream',
            'uploadType' => 'media'
        )
    );

} catch (apiServiceException $e) {
    print 'There was an Analytics API service error '
        . $e->getCode() . ':' . $e->getMessage();

} catch (apiException $e) {
    print 'There was a general API error '
        . $e->getCode() . ':' . $e->getMessage();
}

是否可以post文件名?谢谢!

看起来客户端库的问题比 Google 分析 API 更重要。

consequence of the client libraries stripping the filename from POST body information

Management API Cost Data Upload .csv Doesn't Have Filename

您可以通过不使用客户端库来解决这个问题。

import pycurl
import json

def curlUpload(accountId, webPropertyId, customDataSourceId, csv):
        credential_path = 'PATHTOYOURCREDENTIALSJSONFILE'
        f = open(credential_path)
        googleAccessToken = json.loads(f.read())['access_token']

        c = pycurl.Curl()
        c.setopt(c.URL, 'https://www.googleapis.com/upload/analytics/v3/management/accounts/' + accountId + '/webproperties/' + webPropertyId + '/customDataSources/' + customDataSourceId + '/uploads?access_token=' + googleAccessToken)
        c.setopt(c.HTTPPOST, [
        ('fileupload', (
                c.FORM_FILE, csv,
                c.FORM_CONTENTTYPE, 'application/octet-stream'
        )),
        ])
        c.perform()
        c.close()