How to get the link after uploading to Google Drive via api in python?












0















Here am using google drive API with python to upload files to google drive, how can I get the link of the uploaded file.



I tried print (res['exportLinks']), but it gives me this error.




KeyError: 'exportLinks'




Complete python code



#!/usr/bin/env python

from __future__ import print_function
import os

from apiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools

SCOPES = 'https://www.googleapis.com/auth/drive'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secrets.json', SCOPES)
creds = tools.run_flow(flow, store)
DRIVE = discovery.build('drive', 'v2', http=creds.authorize(Http()))

FILES = (('hello.txt', False),)

for filename, convert in FILES:
metadata = {'title': filename}
res = DRIVE.files().insert(convert=convert, body=metadata,
media_body=filename, fields='mimeType,exportLinks').execute()
if res:
print (res['exportLinks'])


Is there any alternate way to do this.



thanks in advance.










share|improve this question




















  • 1





    KeyError: 'exportLinks' means there is no key 'exportLinks'. Try printing type(res) and res.keys() if its a dictionary.

    – reportgunner
    Dec 28 '18 at 16:17











  • res.keys() gives: ([u'mimeType'])

    – Sumithran
    Dec 28 '18 at 16:23











  • can you please add the result of print(res) to your question ? this is the first time I'm seeing apiclient but I figure you can import json and json.dumps(res)

    – reportgunner
    Dec 28 '18 at 16:33











  • change the fields to * and the version from v2 to v3

    – pinoyyid
    Dec 28 '18 at 17:06
















0















Here am using google drive API with python to upload files to google drive, how can I get the link of the uploaded file.



I tried print (res['exportLinks']), but it gives me this error.




KeyError: 'exportLinks'




Complete python code



#!/usr/bin/env python

from __future__ import print_function
import os

from apiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools

SCOPES = 'https://www.googleapis.com/auth/drive'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secrets.json', SCOPES)
creds = tools.run_flow(flow, store)
DRIVE = discovery.build('drive', 'v2', http=creds.authorize(Http()))

FILES = (('hello.txt', False),)

for filename, convert in FILES:
metadata = {'title': filename}
res = DRIVE.files().insert(convert=convert, body=metadata,
media_body=filename, fields='mimeType,exportLinks').execute()
if res:
print (res['exportLinks'])


Is there any alternate way to do this.



thanks in advance.










share|improve this question




















  • 1





    KeyError: 'exportLinks' means there is no key 'exportLinks'. Try printing type(res) and res.keys() if its a dictionary.

    – reportgunner
    Dec 28 '18 at 16:17











  • res.keys() gives: ([u'mimeType'])

    – Sumithran
    Dec 28 '18 at 16:23











  • can you please add the result of print(res) to your question ? this is the first time I'm seeing apiclient but I figure you can import json and json.dumps(res)

    – reportgunner
    Dec 28 '18 at 16:33











  • change the fields to * and the version from v2 to v3

    – pinoyyid
    Dec 28 '18 at 17:06














0












0








0








Here am using google drive API with python to upload files to google drive, how can I get the link of the uploaded file.



I tried print (res['exportLinks']), but it gives me this error.




KeyError: 'exportLinks'




Complete python code



#!/usr/bin/env python

from __future__ import print_function
import os

from apiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools

SCOPES = 'https://www.googleapis.com/auth/drive'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secrets.json', SCOPES)
creds = tools.run_flow(flow, store)
DRIVE = discovery.build('drive', 'v2', http=creds.authorize(Http()))

FILES = (('hello.txt', False),)

for filename, convert in FILES:
metadata = {'title': filename}
res = DRIVE.files().insert(convert=convert, body=metadata,
media_body=filename, fields='mimeType,exportLinks').execute()
if res:
print (res['exportLinks'])


Is there any alternate way to do this.



thanks in advance.










share|improve this question
















Here am using google drive API with python to upload files to google drive, how can I get the link of the uploaded file.



I tried print (res['exportLinks']), but it gives me this error.




KeyError: 'exportLinks'




Complete python code



#!/usr/bin/env python

from __future__ import print_function
import os

from apiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools

SCOPES = 'https://www.googleapis.com/auth/drive'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secrets.json', SCOPES)
creds = tools.run_flow(flow, store)
DRIVE = discovery.build('drive', 'v2', http=creds.authorize(Http()))

FILES = (('hello.txt', False),)

for filename, convert in FILES:
metadata = {'title': filename}
res = DRIVE.files().insert(convert=convert, body=metadata,
media_body=filename, fields='mimeType,exportLinks').execute()
if res:
print (res['exportLinks'])


Is there any alternate way to do this.



thanks in advance.







python google-drive-sdk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '18 at 16:15







Sumithran

















asked Dec 28 '18 at 16:09









SumithranSumithran

7693822




7693822








  • 1





    KeyError: 'exportLinks' means there is no key 'exportLinks'. Try printing type(res) and res.keys() if its a dictionary.

    – reportgunner
    Dec 28 '18 at 16:17











  • res.keys() gives: ([u'mimeType'])

    – Sumithran
    Dec 28 '18 at 16:23











  • can you please add the result of print(res) to your question ? this is the first time I'm seeing apiclient but I figure you can import json and json.dumps(res)

    – reportgunner
    Dec 28 '18 at 16:33











  • change the fields to * and the version from v2 to v3

    – pinoyyid
    Dec 28 '18 at 17:06














  • 1





    KeyError: 'exportLinks' means there is no key 'exportLinks'. Try printing type(res) and res.keys() if its a dictionary.

    – reportgunner
    Dec 28 '18 at 16:17











  • res.keys() gives: ([u'mimeType'])

    – Sumithran
    Dec 28 '18 at 16:23











  • can you please add the result of print(res) to your question ? this is the first time I'm seeing apiclient but I figure you can import json and json.dumps(res)

    – reportgunner
    Dec 28 '18 at 16:33











  • change the fields to * and the version from v2 to v3

    – pinoyyid
    Dec 28 '18 at 17:06








1




1





KeyError: 'exportLinks' means there is no key 'exportLinks'. Try printing type(res) and res.keys() if its a dictionary.

– reportgunner
Dec 28 '18 at 16:17





KeyError: 'exportLinks' means there is no key 'exportLinks'. Try printing type(res) and res.keys() if its a dictionary.

– reportgunner
Dec 28 '18 at 16:17













res.keys() gives: ([u'mimeType'])

– Sumithran
Dec 28 '18 at 16:23





res.keys() gives: ([u'mimeType'])

– Sumithran
Dec 28 '18 at 16:23













can you please add the result of print(res) to your question ? this is the first time I'm seeing apiclient but I figure you can import json and json.dumps(res)

– reportgunner
Dec 28 '18 at 16:33





can you please add the result of print(res) to your question ? this is the first time I'm seeing apiclient but I figure you can import json and json.dumps(res)

– reportgunner
Dec 28 '18 at 16:33













change the fields to * and the version from v2 to v3

– pinoyyid
Dec 28 '18 at 17:06





change the fields to * and the version from v2 to v3

– pinoyyid
Dec 28 '18 at 17:06












1 Answer
1






active

oldest

votes


















1














I found this page, it says you can get the id of the uploaded file by calling file.get('id') - that is res.get('id') in your case.



try this code



    # ...
res = DRIVE.files().insert(convert=convert, body=metadata,
media_body=filename, fields='mimeType,exportLinks,id').execute() # <-- EDIT: added id here
print('Uploaded file to {url}'.format(url='https://drive.google.com/open?id=' + res.get('id')))





share|improve this answer


























  • res.get('id') returns None

    – Sumithran
    Dec 28 '18 at 17:01











  • was the file uploaded though ? it would make sense to return None if no file was uploaded

    – reportgunner
    Dec 28 '18 at 17:04











  • @Sumithran aha I think I got it, you have to add the id to fields of your .execute(), see my edited answer in a sec

    – reportgunner
    Dec 28 '18 at 17:06











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53961261%2fhow-to-get-the-link-after-uploading-to-google-drive-via-api-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














I found this page, it says you can get the id of the uploaded file by calling file.get('id') - that is res.get('id') in your case.



try this code



    # ...
res = DRIVE.files().insert(convert=convert, body=metadata,
media_body=filename, fields='mimeType,exportLinks,id').execute() # <-- EDIT: added id here
print('Uploaded file to {url}'.format(url='https://drive.google.com/open?id=' + res.get('id')))





share|improve this answer


























  • res.get('id') returns None

    – Sumithran
    Dec 28 '18 at 17:01











  • was the file uploaded though ? it would make sense to return None if no file was uploaded

    – reportgunner
    Dec 28 '18 at 17:04











  • @Sumithran aha I think I got it, you have to add the id to fields of your .execute(), see my edited answer in a sec

    – reportgunner
    Dec 28 '18 at 17:06
















1














I found this page, it says you can get the id of the uploaded file by calling file.get('id') - that is res.get('id') in your case.



try this code



    # ...
res = DRIVE.files().insert(convert=convert, body=metadata,
media_body=filename, fields='mimeType,exportLinks,id').execute() # <-- EDIT: added id here
print('Uploaded file to {url}'.format(url='https://drive.google.com/open?id=' + res.get('id')))





share|improve this answer


























  • res.get('id') returns None

    – Sumithran
    Dec 28 '18 at 17:01











  • was the file uploaded though ? it would make sense to return None if no file was uploaded

    – reportgunner
    Dec 28 '18 at 17:04











  • @Sumithran aha I think I got it, you have to add the id to fields of your .execute(), see my edited answer in a sec

    – reportgunner
    Dec 28 '18 at 17:06














1












1








1







I found this page, it says you can get the id of the uploaded file by calling file.get('id') - that is res.get('id') in your case.



try this code



    # ...
res = DRIVE.files().insert(convert=convert, body=metadata,
media_body=filename, fields='mimeType,exportLinks,id').execute() # <-- EDIT: added id here
print('Uploaded file to {url}'.format(url='https://drive.google.com/open?id=' + res.get('id')))





share|improve this answer















I found this page, it says you can get the id of the uploaded file by calling file.get('id') - that is res.get('id') in your case.



try this code



    # ...
res = DRIVE.files().insert(convert=convert, body=metadata,
media_body=filename, fields='mimeType,exportLinks,id').execute() # <-- EDIT: added id here
print('Uploaded file to {url}'.format(url='https://drive.google.com/open?id=' + res.get('id')))






share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 28 '18 at 17:06

























answered Dec 28 '18 at 16:41









reportgunnerreportgunner

3339




3339













  • res.get('id') returns None

    – Sumithran
    Dec 28 '18 at 17:01











  • was the file uploaded though ? it would make sense to return None if no file was uploaded

    – reportgunner
    Dec 28 '18 at 17:04











  • @Sumithran aha I think I got it, you have to add the id to fields of your .execute(), see my edited answer in a sec

    – reportgunner
    Dec 28 '18 at 17:06



















  • res.get('id') returns None

    – Sumithran
    Dec 28 '18 at 17:01











  • was the file uploaded though ? it would make sense to return None if no file was uploaded

    – reportgunner
    Dec 28 '18 at 17:04











  • @Sumithran aha I think I got it, you have to add the id to fields of your .execute(), see my edited answer in a sec

    – reportgunner
    Dec 28 '18 at 17:06

















res.get('id') returns None

– Sumithran
Dec 28 '18 at 17:01





res.get('id') returns None

– Sumithran
Dec 28 '18 at 17:01













was the file uploaded though ? it would make sense to return None if no file was uploaded

– reportgunner
Dec 28 '18 at 17:04





was the file uploaded though ? it would make sense to return None if no file was uploaded

– reportgunner
Dec 28 '18 at 17:04













@Sumithran aha I think I got it, you have to add the id to fields of your .execute(), see my edited answer in a sec

– reportgunner
Dec 28 '18 at 17:06





@Sumithran aha I think I got it, you have to add the id to fields of your .execute(), see my edited answer in a sec

– reportgunner
Dec 28 '18 at 17:06


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53961261%2fhow-to-get-the-link-after-uploading-to-google-drive-via-api-in-python%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas