uploadData Google Analytics中的文件名

时间:2016-09-16 17:47:13

标签: php google-analytics google-analytics-api

我需要使用API​​将CSV上传到Google Analytics。一切顺利,但列中的文件名显示"未知文件名"。

File name is unknown

这是我的代码:

$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();
}

是否可以使用flename发布文件?谢谢!

1 个答案:

答案 0 :(得分:0)

对于客户端库以及Google分析API,它看起来更像是一个问题。

  

客户端库从POST正文信息中剥离文件名的结果

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()