I haven't used io within pandas to access google analytic's API for a few weeks but it had been working fine to my knowledge historically without hiccups. I ran it again today and it looks as though the tools.run syntax is deprecated, so I made a pull and replaced tools.py with this update and I've changed to auth.py within pandas to be:
def authenticate(flow, storage=None):
"""
Try to retrieve a valid set of credentials from the token store if possible
Otherwise use the given authentication flow to obtain new credentials
and return an authenticated http object
Parameters
----------
flow : authentication workflow
storage: token storage, default None
"""
http = httplib2.Http()
# Prepare credentials, and authorize HTTP object with them.
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = tools.run_flow(flow, storage, FLAGS)
http = credentials.authorize(http)
return http
I have a feeling my usage of FLAGS there is incorrect.
Any help? Thanks!
Here's my code and the error:
df = ga.read_ga(
account_id = id,
profile_id = profile,
property_id = property,
metrics = ['transactionRevenue', 'transactions'],
dimensions = ['transactionId', 'city', 'region', 'date', 'hour', 'minute', 'cityId'],
start_date = "2015-07-11",
end_date = "2015-07-16",
index_col = 0,
parse_dates = {'new_date': [3,4,5]})
The error thrown up:
C:\Users\mburke\AppData\Local\Continuum\Anaconda64\lib\site-packages\pandas\io\auth.py in authenticate(flow, storage)
106 credentials = storage.get()
107 if credentials is None or credentials.invalid:
--> 108 credentials = tools.run_flow(flow, storage, FLAGS)
109
110 http = credentials.authorize(http)
C:\Users\mburke\AppData\Local\Continuum\Anaconda64\lib\site-packages\oauth2client\util.pyc in positional_wrapper(*args, **kwargs)
140 else: # IGNORE
141 pass
--> 142 return wrapped(*args, **kwargs)
143 return positional_wrapper
144
C:\Users\mburke\AppData\Local\Continuum\Anaconda64\lib\site-packages\oauth2client\tools.pyc in run_flow(flow, storage, flags, http)
148 logging.getLogger().setLevel(getattr(logging, flags.logging_level))
--> 149 if not flags.noauth_local_webserver:
150 success = False
151 port_number = 0
C:\Users\mburke\AppData\Local\Continuum\Anaconda64\lib\site-packages\python_gflags-2.0-py2.7.egg\gflags.pyc in __getattr__(self, name)
1057 fl = self.FlagDict()
1058 if name not in fl:
-> 1059 raise AttributeError(name)
1060 return fl[name].value
1061
AttributeError: noauth_local_webserver
I did a little digging and you are correct in your assumption that the usage of FLAGS is incorrect. The docstring for tools.run_flow() states:
flags: ``argparse.Namespace``, The command-line flags. This is the
object returned from calling ``parse_args()`` on
``argparse.ArgumentParser`` as described above.
The quick-n-dirty fix would be something like this:
credentials = tools.run_flow(flow, storage, tools.argparser.parse_args([]))
I believe a more robust solution would be for the maintainers of pandas.io to update it to the new workflow if tools.run is really deprecated.
Related
It looks like the error is related to an invalid API key or access token. Everything I did is correct and mentioned below what steps are taken by me:
I've created https://developers.kite.trade/ (from the Zerodha Kite Connect dashboard)
Here is Zerodha API key created image
To get data from Zerodha in Python, I am trying the Zerodha Kite Connect API. Kite Connect is a set of REST-like APIs that expose many capabilities required to build a complete investment and trading platform. To use the API, I first needed to create a Zerodha account and then applied for API access. After I have received your API key, I can use it to make requests to the Kite Connect API using a Python library such as kiteconnect or kiteconnect-python.
Here is an example of how you could use the kiteconnect library to get historical data for a stock:
This python code:
from kiteconnect import KiteConnect
import datetime
kite = KiteConnect(api_key='0cv9cnax7bmgjclh')
# Get historical data for a stock
today = datetime.datetime.now().date()
historical_data = kite.historical_data(
instrument_token=6048, # Instrument token of a stock
from_date=today - datetime.timedelta(days=365), # From date
to_date=today, # To date
interval="daily" # Interval (minute, hourly, daily, weekly, monthly, yearly)
)
print(historical_data)
Error:
---------------------------------------------------------------------------
InputException Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_18768\3958108260.py in <module>
6 # Get historical data for a stock
7 today = datetime.datetime.now().date()
----> 8 historical_data = kite.historical_data(
9 instrument_token=6048, # Instrument token of a stock
10 from_date=today - datetime.timedelta(days=365), # From date
~\anaconda3\lib\site-packages\kiteconnect\connect.py in historical_data(self, instrument_token, from_date, to_date, interval, continuous, oi)
629 to_date_string = to_date.strftime(date_string_format) if type(to_date) == datetime.datetime else to_date
630
--> 631 data = self._get("market.historical",
632 url_args={"instrument_token": instrument_token, "interval": interval},
633 params={
~\anaconda3\lib\site-packages\kiteconnect\connect.py in _get(self, route, url_args, params, is_json)
849 def _get(self, route, url_args=None, params=None, is_json=False):
850 """Alias for sending a GET request."""
--> 851 return self._request(route, "GET", url_args=url_args, params=params, is_json=is_json)
852
853 def _post(self, route, url_args=None, params=None, is_json=False, query_params=None):
~\anaconda3\lib\site-packages\kiteconnect\connect.py in _request(self, route, method, url_args, params, is_json, query_params)
925 # native Kite errors
926 exp = getattr(ex, data.get("error_type"), ex.GeneralException)
--> 927 raise exp(data["message"], code=r.status_code)
928
929 return data["data"]
InputException: Invalid `api_key` or `access_token`.
I am trying to get historical data via API from Zerodha.
I was developing a price prediction model that requires Tiingo but there seems to be problem in the API authentification. I used the OS access the Tiingo API.
`
api_key =os.environ.get('TIINGO_API_KEY')
df=pdr.get_data_tiingo('AAPL',api_key)
df=pd.read_csv('AAPL.csv')
print(df.tail())
The error I got looks like:
~\AppData\Local\Temp/ipykernel_9920/1017009006.py in <module>
1 api_key =os.environ.get('TIINGO_API_KEY')
----> 2 df=pdr.get_data_tiingo('AAPL',api_key)
3 df=pd.read_csv('AAPL.csv')
4 print(df.tail())
~\anaconda3\lib\site-packages\pandas_datareader\data.py in get_data_tiingo(*args, **kwargs)
118
119 def get_data_tiingo(*args, **kwargs):
--> 120 return TiingoDailyReader(*args, **kwargs).read()
121
122
~\anaconda3\lib\site-packages\pandas_datareader\tiingo.py in __init__(self, symbols, start, end, retry_count, pause, timeout, session, freq, api_key)
181 api_key = os.getenv("TIINGO_API_KEY")
182 if not api_key or not isinstance(api_key, str):
--> 183 raise ValueError(
184 "The tiingo API key must be provided either "
185 "through the api_key variable or through the "
ValueError: The tiingo API key must be provided either through the api_key variable or through the environmental variable TIINGO_API_KEY.
Any assistance is highly appreciated
It seems api_key is coming as None. You should check that.
I could manage to get access to Azure resources with the code bellow:
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.compute import ComputeManagementClient
from azure.graphrbac import GraphRbacManagementClient
subscription_id = "aaaa"
tenant_id = "bbbb"
credentials = ServicePrincipalCredentials(
client_id="cccc",
secret="dddd",
tenant=tenant_id
)
client = ResourceManagementClient(credentials, subscription_id)
for item in client.resource_groups.list():
print item
compute_client = ComputeManagementClient(credentials, subscription_id)
disks = compute_client.disks.list()
for disk in disks:
print disk
But I can't access Azure AD with the same code!!! Is there a different way to access it? Why is it different?! See the code bellow:
graphrbac_client = GraphRbacManagementClient(credentials, subscription_id)
for item in graphrbac_client.groups.list():
print item
Error:
GraphErrorExceptionTraceback (most recent call last)
in ()
1 graphrbac_client = GraphRbacManagementClient(credentials, subscription_id)
2
----> 3 for item in graphrbac_client.groups.list():
4 print item
/home/andre/.local/lib/python2.7/site-packages/msrest/paging.pyc in
next(self)
129 return response
130 else:
--> 131 self.advance_page()
132 return self.next()
133
/home/andre/.local/lib/python2.7/site-packages/msrest/paging.pyc in
advance_page(self)
115 raise StopIteration("End of paging")
116 self._current_page_iter_index = 0
--> 117 self._response = self._get_next(self.next_link)
118 self._derserializer(self, self._response)
119 return self.current_page
/home/andre/.local/lib/python2.7/site-packages/azure/graphrbac/operations/groups_operations.pyc
in internal_paging(next_link, raw)
336
337 if response.status_code not in [200]:
--> 338 raise models.GraphErrorException(self._deserialize, response)
339
340 return response
GraphErrorException: Access Token missing or malformed.
azure-common version = 1.1.14
Access Token missing or malformed.
ComputeManagementClient resource path is https://management.azure.com
But for GraphRbacManagementClient the resource path is https://graph.windows.net. So you got the exception.
How to access Azure AD with Python SDK?
You could get the answer from this link. The following code is the snippet from the document.
from azure.graphrbac import GraphRbacManagementClient
from azure.common.credentials import UserPassCredentials
# See above for details on creating different types of AAD credentials
credentials = UserPassCredentials(
'user#domain.com', # Your user
'my_password', # Your password
resource="https://graph.windows.net"
)
tenant_id = "myad.onmicrosoft.com"
graphrbac_client = GraphRbacManagementClient(
credentials,
tenant_id
)
I don't know how to add note in a cell by Google Sheet API:
https://developers.google.com/sheets/api/guides/values#writing_multiple_ranges
I read info of Google Sheet API: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets#CellData
I also read this question but it's still not working
Is it possible to use the Google Spreadsheet API to add a comment in a cell?
I don't know how to set note in the value.
I need someone help ~!
Thanks a lot.
from __future__ import print_function
import httplib2
import os
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/sheets.googleapis.com-python-quickstart.json
# Scope type --> https://developers.google.com/sheets/api/guides/authorizing
SCOPES = 'https://www.googleapis.com/auth/spreadsheets'
CLIENT_SECRET_FILE = 'client_secret.json' # credentials file name
APPLICATION_NAME = 'auto_update_caspar'
def get_credentials():
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'auto_update_client_caspar.json')
store = Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def main():
"""Shows basic usage of the Sheets API.
Creates a Sheets API service object and prints the names and majors of
students in a sample spreadsheet:
https://docs.google.com/spreadsheets/d/1k7OmDU_QUrCPVmsEpWFRCj-4BOu6PcUb7-SQlA7T_8I/edit
"""
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
discoveryUrl = ('https://sheets.googleapis.com/$discovery/rest?'
'version=v4')
service = discovery.build('sheets', 'v4', http=http,
discoveryServiceUrl=discoveryUrl)
spreadsheetId = '1lEJeSNe5T3rNXEEMjY4D04QkoW-ngOeiFo40_S4H4FI'
rangeName = 'stress_temp!A3'
result = service.spreadsheets().values().get(
spreadsheetId=spreadsheetId, range=rangeName).execute()
print (result)
values = result.get('values', [])
if not values:
print('No data found.')
else:
print('Name, Major:')
for row in values:
# Print columns A and E, which correspond to indices 0 and 4.
print('%s' % row[0])
value_input_option = 'RAW'
range_name = 'stress_temp!A3'
values = [['aa']]
data = [
{
'range': range_name,
'values': values
},
]`enter code here`
body = {
'valueInputOption': value_input_option,
'data': data
}
result = service.spreadsheets().values().batchUpdate(
spreadsheetId=spreadsheetId, body=body).execute()
if __name__ == '__main__':
main()
In Python, I use this piece of code and it work.
from googleapiclient.discovery import build
from google.oauth2 import service_account
SCOPES = ["https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/spreadsheets"]
SERVICE_ACCOUNT_FILE = 'credentials.json'
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = build('sheets', 'v4', credentials=credentials)
spreadsheetId = "O188999ITC"
sheetid_src = "123a"
notes = {
"updateCells": {
"range": {
"sheetId": sheetid_src,
"startRowIndex": 1,
"endRowIndex: 1,
"startColumnIndex": 1,
"endColumnIndex": 1
},
"rows": [
{
"values": [
{
"note": "my note"
}
]
}
],
"fields": "note"
}
}
body = {"requests":[notes]}
result = service.spreadsheets().values().batchUpdate(
spreadsheetId=spreadsheetId, body=body).execute()
Note are available in API with cellData, so you have to update values of differents cells with the keyword "Notes".
You can find more information here :
https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/cells#celldata
I don't see any documentation regarding your issue. You may refer with this SO thread however it discussed how to get a note using getNote(). In your case, you need to use Google Apps Script and use the methods:
setNote(note) - Sets the note to the given value.
setNotes(notes) - Sets a rectangular grid of notes (must match dimensions of this range).
You may also check into this related reported issue. Hope this helps!
I am using python google drive api, python 2.7.10 on windows 10.
I am setting a instance variable to a drive service. The problem arises when I try to run one of the the drive's service's methods self.service.files().list(). I believe that python is passing both the object self and the string "title = 'Door_Photos' and mimeType = 'application/vnd.google-apps.folder'"Is there away to stop python from doing this?
class doorDrive():
def __init__(self, scopes = 'https://www.googleapis.com/auth/drive.metadata.readonly',
secretFile = 'client_secret.json',
appName = 'Door Cam'):
self.SCOPES = scopes
self.CLIENT_SECRET_FILE = secretFile
self.APPLICATION_NAME = appName
self.photoFolderId = ''
creds = self.getCreds()
http = creds.authorize(httplib2.Http())
self.service = discovery.build('drive', 'v2', http=http)
self.initFolder()
def getCreds(self):
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'drive-python-quickstart.json')
store = oauth2client.file.Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(self.CLIENT_SECRET_FILE, self.SCOPES)
flow.user_agent = self.APPLICATION_NAME
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def initFolder(self):
folders = self.service.files().list("title = 'Door_Photos' and mimeType = 'application/vnd.google-apps.folder'").execute()['items']
Your last line is passing your query string directly to list(), but you should probably be passing it in by keyword, like this:
def initFolder(self):
folders = self.service.files().list(q="title = 'Door_Photos' and mimeType = 'application/vnd.google-apps.folder'").execute()['items']
Notice the q= in the front of your query now. That will make Python send it in as a keyword argument instead of a positional argument. I think your error is cascading further downward because the first argument to that function is actually orderBy.
You can see the specification here: https://developers.google.com/resources/api-libraries/documentation/drive/v2/python/latest/drive_v2.files.html#list