如何使用手表订阅Google云端硬盘上的更改

时间:2018-11-15 15:17:08

标签: google-drive-api google-docs-api

我非常想尝试订阅Google驱动器文件夹上的更改。我的python3代码如下:         SCOPES ='https://www.googleapis.com/auth/drive.readonly'     store = file.Storage('storage.json')

credentials = store.get()
if not credentials or credentials.invalid:
    flow = client.flow_from_clientsecrets('client_id.json', SCOPES)
    credentials = tools.run_flow(flow, store)

# This starts the authorization process
DRIVE = discovery.build('drive', 'v3', http=credentials.authorize(Http()))

try:
    with open('saved_start_page_token.json') as json_data:
        d = json.load(json_data)
        try:
            saved_start_page_token = d["startPageToken"]
        except KeyError:
            saved_start_page_token = d["newStartPageToken"]
        print("Using saved token: %s" % saved_start_page_token)

except FileNotFoundError:
    response = DRIVE.changes().getStartPageToken().execute()
    with open("saved_start_page_token.json", "w") as token:
        json.dump(response, token)
    saved_start_page_token = response.get('startPageToken')
    print('Start token: %s' % saved_start_page_token)

body = dict()
body["kind"] = "api#channel"
body["id"] = str(uuid.uuid4())  # TODO: do I have to do something with this channel id?
print(body["id"])
body["resourceId"] = 'web_hook'
body["resourceUri"] = 'https://meg-wm-it-change.appspot.com/notifications/'
json_body = json.dumps(body)
print(json_body)

request = DRIVE.changes().watch(pageToken = saved_start_page_token, body=json_body)
response = request.execute()

return response.body

除了抛出错误

googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/drive/v3/changes/watch?pageToken=163958&alt=json returned "entity.resource">

我无法完全解密。我确定我的问题将是不理解文档(即,我不了解参数是否与该请求的主体相对,并且找不到任何代码示例),但是任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

如果其他人在这里徘徊,我将发布我对自己的问题找到的答案:

string filePath = @"D:\Aggregate_Minute_AAPL.txt";
var records = (from line in File.ReadLines(filePath).AsParallel()                    
               select line);
    List<string> str = records.ToList();
    str.ForEach(x =>
    {
         string result = x;
         result = result.TrimStart('[').TrimEnd(']');
         var jsonString = Newtonsoft.Json.JsonConvert.DeserializeObject<List<LiveAMData>>(x);
         foreach (var item in jsonString)
         {
             string value = "";
             string dirPath = @"D:\COMB1\MinuteAggregates";
             string[] fileNames = null;
             fileNames = System.IO.Directory.GetFiles(dirPath, item.sym+"_*.txt", System.IO.SearchOption.AllDirectories);
             if(fileNames.Length > 0)
             {
                 string _fileName = fileNames[0];
                 var lineList = System.IO.File.ReadAllLines(_fileName).ToList();
                 lineList.RemoveAt(0);
                 var _item = lineList[lineList.Count - 1];
                 if (!_item.Contains(item.sym))
                 {
                      lineList.RemoveAt(lineList.Count - 1);
                 }
                 System.IO.File.WriteAllLines((_fileName), lineList.ToArray());
                 value = $"{item.sym},{item.s},{item.o},{item.h},{item.c},{item.l},{item.v}{Environment.NewLine}";
                 using (System.IO.StreamWriter sw = System.IO.File.AppendText(_fileName))
                 {
                      sw.Write(value);
                 }
              }
          }
 });