File "quickstart.py", line 9, in <module> - python-3.x

I am new in Python and trying use Google Drive Apis, but facing this issue. Error I am getting after running python quickstart.py
Traceback (most recent call last):
File "quickstart.py", line 9, in <module>
creds = store.get()
File "/usr/local/lib/python3.6/site-packages/oauth2client/client.py", line 407, in get
return self.locked_get()
File "/usr/local/lib/python3.6/site-packages/oauth2client/file.py", line 54, in locked_get
credentials = client.Credentials.new_from_json(content)
File "/usr/local/lib/python3.6/site-packages/oauth2client/client.py", line 302, in new_from_json
module_name = data['_module']
KeyError: '_module'
I have generated client_secret.json file as per the Python Quickstart tutorial.
All the file are in the same directory as that of quickstart.py.
Here is how my quickstart.py file looks.
from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
# Setup the Drive v3 API
SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly'
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('drive', 'v3', http=creds.authorize(Http()))
# Call the Drive v3 API
results = service.files().list(
pageSize=10, fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])
if not items:
print('No files found.')
else:
print('Files:')
for item in items:
print('{0} ({1})'.format(item['name'], item['id']))
UPDATE:
I checked and it turns out that credentials.json file is auto-generated on the first run and for some reason, this is not happening.
KeyError: '_module'
This key _module is suppose to be present in credentials.json file and that is why this error is thrown. Not sure what is missing. Can someone please tell me how to resolve this issue.

Similar issue here Try to remove both files from your directory - "credentials.json" and "client_secret.json". Then re-generate your credentials and re-create "client_secret.json", this worked for me.

Related

upload multiple files at once from google drive to google cloud storage

my objective is to identify some files in google drive and upload them sequentially to a google storage bucket. I am using a google 'cloud function' to do this and have already done tests to confirm that the connection is working properly.
The issue I have seems to relate to how I get the name of the file - it is returning a 'none type' value. Please see my code below
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from google.cloud import storage
import io
from googleapiclient import discovery
from pandas.io.json import json_normalize
import google.auth
import re
import logging
# Confirming Oauth for Drive API#
SCOPES = [ 'https://www.googleapis.com/auth/drive']
creds, project = google.auth.default(scopes=SCOPES)
service = build('drive', 'v3', credentials=creds)
# Confirming Oauth #
storage_client = storage.Client()
## target Bucket #
bucket_name = 'my-bucket'
bucket = storage_client.bucket(bucket_name)
get_bucket = storage_client.get_bucket(bucket_name)
team_drive_loc =
'https://drive.google.com/drive/u/0/folders/xxxxxxxxxxx'
team_drive_parent_folder ='xxxxxxxxxxxxxxxxxA'
#bucket = storage_client.bucket(bucket_name)
query= "name contains 'Customer' and name contains '2022' "
drive_files = service.files().list(q= query,
driveId =team_drive_parent_folder,
supportsAllDrives= True,
includeItemsFromAllDrives= True,
corpora ='drive',fields="files(id,name)").execute()
for file in drive_files:
source_file_name =service.get(fileId=fileId, fields="files(name)").execute()["files(name)"]
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob("incoming/iri/IRI_Updates/Ongoing_Sales_Data/2022/" +
source_file_name)
blob.upload_from_filename(source_file_name)
logging.info('Uploaded {} ...'.format(source_file_name))
...And this is the error i get. If anyone can help me source the file name correctly and upload to the gcs bucket, that would be great
Exception on / [POST] Traceback (most recent call last): File "/layers/google.python.pip/pip/lib/python3.7/site-packages/flask/app.py", line 2073, in wsgi_app response = self.full_dispatch_request() File "/layers/google.python.pip/pip/lib/python3.7/site-packages/flask/app.py", line 1518, in full_dispatch_request rv = self.handle_user_exception(e) File "/layers/google.python.pip/pip/lib/python3.7/site-packages/flask/app.py", line 1516, in full_dispatch_request rv = self.dispatch_request() File "/layers/google.python.pip/pip/lib/python3.7/site-packages/flask/app.py", line 1502, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args) File "/layers/google.python.pip/pip/lib/python3.7/site-packages/functions_framework/__init__.py", line 171, in view_func function(data, context) File "/workspace/main.py", line 86, in iri_data_sync source_file_name =service.get(fileId=fileId, fields="files(name)").execute()["files(name)"] AttributeError: 'Resource' object has no attribute 'get'
The error message is telling you the issue
'Resource' object has no attribute 'get'
its not service.get its service.files().get
source_file_name =service.files().get(fileId=fileId, fields="files(name)").execute()["files(name)"]
you actually had it right with service.files().list you just removed forgot it with the get request.

Sending my first message on the slack API

I registered my first app, and it looks like this:
All of the fields are populated below the screen shot.
Now, I have some basic python code using the examples found on their repo.
I create the following test script:
import traceback
app_id = 'FAKE.VALUE'
client_id = 'FAKE.VALUE'
client_secret = 'FAKE.VALUE'
signin_secret = 'FAKE.VALUE'
verification_token = 'FAKE.VALUE'
items = locals()
import os
import slack
items = locals().copy()
for k in items:
if '__' not in k:
val = items[k]
try:
client = slack.WebClient(token=val)
response = client.chat_postMessage(
channel='CE476K9HT',
text='Hello-----' + str(val))
print(response)
except:
print(k)
traceback.print_exc()
print('-'*50)
But all of the responses I get say:
The server responses with: {'ok':False,'error':'invalid_auth'}
For some reason, is it necessary to use path variables?
It is unclear to me what type of auth is required here.
After doing what Erik suggested,
I have a xoxp code and registered the redirect url to http://localhost.
and added the following scopes:
and updated my code to look like:
oauth_token ='xoxp-*****************'
import os
import slack
items = locals().copy()
client = slack.WebClient(token=oauth_token)
response = client.chat_postMessage(
channel='my_channel_id',
text='Hello-----')
I got my channel ID from the url:
https://app.slack.com/client/FOO/my_channel_id
When I run my code, I get the following back:
Traceback (most recent call last):
File "/home/usr/git/slack_messaging/slack_messaging.py", line 20, in <module>
text='Hello-----')
File "/home/usr/git/python-slackclient/slack/web/client.py", line 382, in chat_postMessage
return self.api_call("chat.postMessage", json=kwargs)
File "/home/usr/git/python-slackclient/slack/web/base_client.py", line 172, in api_call
return self._event_loop.run_until_complete(future)
File "/home/usr/anaconda2/envs/beer/lib/python3.7/asyncio/base_events.py", line 573, in run_until_complete
return future.result()
File "/home/usr/git/python-slackclient/slack/web/base_client.py", line 241, in _send
return SlackResponse(**{**data, **res}).validate()
File "/home/usr/git/python-slackclient/slack/web/slack_response.py", line 176, in validate
raise e.SlackApiError(message=msg, response=self)
slack.errors.SlackApiError: The request to the Slack API failed.
The server responded with: {'ok': False, 'error': 'missing_scope', 'needed': 'chat:write:user', 'provided': 'admin,identify'}
Process finished with exit code 1
You need two things to make your script work.
1. OAuth token
You need a valid OAuth token and provide it when initializing the Slack Client:
client = slack.WebClient(token="xoxb-xxx")
To get a token you need to install your Slack app to a workspace. You can do that on the app management page under "Install App". Your OAuth token will also be displayed on that page once you installed it.
2. Permissions
Your Oauth Token / Slack app needs to have the permission to post messages. On the app management pages go to "OAuth & permission" and add the needed permission to your app:. e.g. chat:write:user for user tokens.
Note that you need to reinstall your app every time to add a permission.

Unresolved reference 'MediaFileUpload'

I am trying to use the Google driveApi in python while I get this error, I installed all the necessary dependecies, but I am still getting this problem for Unresolved reference:
Traceback (most recent call last):
File "C:/Users/User/PycharmProjects/dandalo/GoogleDriveAPI.py", line 2, in
<module>
class UploadToGoogleDrive:
File "C:/Users/User/PycharmProjects/dandalo/GoogleDriveAPI.py", line 20, in
UploadToGoogleDrive
media = googleapiclient.MediaFileUpload('files/photo.jpg',
AttributeError: module 'googleapiclient' has no attribute 'MediaFileUpload'
This is my code, what am I doing wrong?
class UploadToGoogleDrive:
from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
# Setup the Drive v3 API
SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly'
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
creds = tools.run_flow(flow, store)
service = build('drive', 'v3', http=creds.authorize(Http()))
file_metadata = {'name': 'photo.jpg'}
media = MediaFileUpload('files/photo.jpg',
mimetype='image/jpeg')
file = drive_service.files().create(body=file_metadata,
media_body=media,
fields='id').execute()
print('File ID: %s' % file.get('id'))
You're trying to import MediaFileUpload from googleapiclient.
MediaFileUpload comes from the http module in googleapiclient
Try this instead
media = googleapiclient.http.MediaFileUpload('files/photo.jpg')
just add this to your code
from apiclient.http import MediaFileUpload

What is wrong when azure.datalake.store commands gives LISTERROR response?

Hi I am trying to access my Data Lake Store from a python program locally from my desktop. I get this strange error while executing the line
adl.ls('/')
The output is
Traceback (most recent call last): File
"C:\Users\StefanFrost\adltest.py", line 31, in
adl.ls('/') File "C:\Python34\lib\site-packages\azure\datalake\store\core.py", line
124, in ls
files = self._ls(path) File "C:\Python34\lib\site-packages\azure\datalake\store\core.py", line
115, in _ls
out = self.azure.call('LISTSTATUS', key) File "C:\Python34\lib\site-packages\azure\datalake\store\lib.py", line 362,
in call
self.log_response_and_raise(r, err) File "C:\Python34\lib\site-packages\azure\datalake\store\lib.py", line 290,
in log_response_and_raise
raise exception azure.datalake.store.exceptions.DatalakeRESTException: Data-lake REST
exception: LISTSTATUS, .
I know that the firewall is open because the service is responding and I know that the supplied correct credentials, because I get unauthorized output when using the incorrect credentials. Below is the complete code.
from azure.datalake.store import core, lib, multithread
import logging, getpass, pprint, uuid, time
tenant_id = '<My Tenant Id>'
client_id = '<My Azure User Id>'
app_pwd = '<My Azure Password>'
datalake_nm = '<My Data Lake Store Name>'
token = lib.auth(tenant_id, client_id, app_pwd)
adl = core.AzureDLFileSystem(token, store_name=datalake_nm)
adl.ls('/')

Python - HttpError when using google drive API

I am using python 3.4 to leverage the Google API to access and read files from a users google drive. If a user has already used the app before they should have a credentials file so I was hoping to be able to test if the credentials are still valid by attempting to list the files on the users drive. The idea being if this errors then the app knows it needs to ask for access again.
After a lot of searching I've tried to piece together code from the following Examples:
Google API commands
Google Example
I currently have the following pieces of code:
import httplib2
from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import AccessTokenRefreshError
from oauth2client.client import OAuth2WebServerFlow
def getAccess():
flow = OAuth2WebServerFlow(client_id, client_secret, scope, redirect_uri="urn:ietf:wg:oauth:2.0:oob")
auth_uri = flow.step1_get_authorize_url()
print("Please go to the following webpage and copy and paste the access key onto the command line:\n" + auth_uri + "\n")
code = input("Please enter your access code:\n")
credentials = flow.step2_exchange(code)
storage.put(credentials)
client_id = MYCLIENT_ID
client_secret = MYCLIENT_SECRET
scope = "https://www.googleapis.com/auth/drive"
storage = Storage('~/credentials.dat')
credentials = storage.get()
if credentials is None or credentials.invalid:
getAccess()
else:
try:
http = httplib2.Http()
http = credentials.authorize(http)
service = build('drive', 'v2', http=http)
param = {}
service.files().list(**param).execute()
except:
getAccess()
However the service.files().list(**param).execute() line produces the following error message:
Traceback (most recent call last):
File "GoogleAuth.py", line 64, in <module>
service.files().list(**param).execute()
File "C:\Anaconda3\lib\site-packages\oauth2client\util.py", line 137, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Anaconda3\lib\site-packages\googleapiclient\http.py", line 729, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError
I've tried playing around with a few different combinations such as:
service.files().list().execute()
service.apps().list().execute()
However I still get the same error message. Any idea what's going on ?
Issue was that
service = build('drive', 'v2')
Should have been
service = build('drive', 'v2', http=http)

Resources