Flask on GAE - Connect to Google API - Can't use JSON file - python

I'm fairly new to Flask, GAE and the use of API. I'm trying to build a basic Web App that can connect to one of Google's API.
My folder structure looks like this (I've kept it to the main files):
app-webemotions:
-app.yaml
-main.py
-lib
--sentimentanalysis.py
-static
--credential.json
Everything is working but providing the json file for the credentials. My understanding is that there's a couple of ways to do it:
1) Setting up the GOOGLE_APPLICATION_CREDENTIALS environment variable to the destination of my file in app.yaml
2) Requesting the file through my script (sentimentanalysis.py)
Unfortunately, I haven't been able to make any of those work.
Option 1):
In app.yaml I have the line:
env_variables:
GOOGLE_APPLICATION_CREDENTIALS: static/key/credentials.json
I then run my code through dev_appserver.py . and get the following error:
ApplicationDefaultCredentialsError: File static/key/credentials.json (pointed by GOOGLE_APPLICATION_CREDENTIALS environment variable) does not exist!
Option 2):
I have a line of code in my script sentimentanalysis.py:
scope = ['https://www.googleapis.com/auth/cloud-platform']
credentials = ServiceAccountCredentials.from_json_keyfile_name('/static/credentials.json', scope)
And when running the code I get the following error:
raise IOError(errno.EACCES, 'file not accessible', filename)
IOError: [Errno 13] file not accessible: '/static/credentials.json'
INFO 2016-08-06 04:10:51,678 module.py:788] default: "POST /Sentiment-analysis HTTP/1.1" 500 -
Question:
So it looks like regardless of the method I'm using, I'm not able to provide the right path to the JSON file
My question is to know first if any of the above options is the right option and if yes, what am I doing wrong? If they are not the right options, what would you recommend?
Apologies if this has already been asked, I've tried to find an answer for a few hours now and haven't been able to crack it...
Thank you!

If you are running on Google App Engine, then your code automatically has the credentials it needs. Do not set GOOGLE_APPLICATION_CREDENTIALS and do not call .from_json_keyfile_name. Instead, call:
credentials = GoogleCredentials.get_application_default()
As shown here:
https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/bigquery/api/getting_started.py

set GOOGLE_APPLICATION_CREDENTIALS=credentials.json
use this command if you are using cmd

Related

Unable to generate a share link via a script and Synology API

I'm hitting a wall with the python script at https://stackoverflow.com/a/60946539/1641112. I've added the script to a file called test.py3.
I'm running this command directly on my Synology drive with an admin account: python test.py3 /volume2/dir/key-basic-preferred.pdf --debug
The relevant part of the debug output:
Get(): "request = entry.cgi?api=SYNO.FileStation.Sharing&version=3&method=create&path="/volume2/dir/key-basic-preferred.pdf""
GET: "http://127.0.0.1:5000/webapi/entry.cgi?api=SYNO.FileStation.Sharing&version=3&method=create&path="/volume2/dir/key-basic-preferred.pdf"&_sid=011o6xaJaxvLsKCJ4N91278"
GET: "<Response [200]>"
ERROR: Get (entry.cgi?api=SYNO.FileStation.Sharing&version=3&method=create&path="/volume2/dir/key-basic-preferred.pdf"&_sid=011o6xaJaxvLsKCJ4N91278):
Error: 408: Unknown error
The 408 error is an unknown file according to the API docs.
However, I know that file exists. I've tried URL encoding the path. I've tried with and without quotes. I've tried noodling with the python code (don't know python) to change the quotations and api version to 1 instead of 3 but I'm not having any luck.
Finally figured it out. The file paths are relative to the File Station. So if you have a folder in file station called "backup" and a file in "backup" call "blah.txt", the path to the file is /backup/blah.txt

TOR with python stem (basic) - 'tor' not in PATH

I'm trying to get an .onion website's content to python, a little research showed that 'stem'
and as i am running this tutorial script, or more specifically, when i'm trying to use stem.process.launch_tor_with_config, i get this error:
'tor' isn't available on your system. Maybe it's not in your PATH?
I supposed to have some sort of tor process installed, I got the tor browser bundle and put the /Tor library (with the tor.exe) in it in my path, and it's not helping...
Obviously I'm missing something VERY BASIC, please advise....
Many thanks...
I had the same issue on my Mac. Try specifying exactly where the tor file is located to the tor_cmd parameter:
tor_process = stem.process.launch_tor_with_config(
tor_cmd = '/Applications/TorBrowser.app/Tor/tor.real',
config = { SocksPort': str(SOCKS_PORT),
'ExitNodes': '{ru}',},
init_msg_handler = print_bootstrap_lines,
)

Google API client secrets error (Python)

I want to retrieve data from Google analytics. I have created a service account in the console and I am using Google's Python (hello_analytics_api_v3.py) code to access the data.
I have copied the client_secrets.json into my folder but get this error:
*SystemExit:
WARNING: Please configure OAuth 2.0
To make this sample run you will need to populate the client_secrets.json file
found at:*
What should I do? I am using Python 2.7.
Ensure the terminal is pointing to the same path directory as your client_secrets.json file.
i.e. type pwd in the console you're using to call the script and the output should match the directory of where client_secrets.json is stored.
I was having this exact issue and deleted the credentials to my project and creating new ones using the 'OAuth client ID' option. Follow step one of this page closley https://developers.google.com/analytics/devguides/config/mgmt/v3/quickstart/installed-py
I also found a syntax error in the sample code provided by google
The Lines:
print 'View (Profile): %s' % results.get('profileInfo').get('profileName')
print 'Total Sessions: %s' % results.get('rows')[0][0]
Should read:
print ('View (Profile): %s' % (results.get('profileInfo').get('profileName')))
print ('Total Sessions: %s' % (results.get('rows')[0][0]))
At least this solved it for me. Also, make sure the client_secrets.json is in the same directory as your python script.
In the sample code at https://developers.google.com/youtube/v3/guides/uploading_a_video the call to flow_from_clientsecrets() passes CLIENT_SECRETS_FILE as a relative path.
To fix it, force the CLIENT_SECRETS_FILE argument to be an absolute path:
def get_authenticated_service(args):
flow = flow_from_clientsecrets(
os.path.abspath(os.path.join(
os.path.dirname(__file__),CLIENT_SECRETS_FILE)),
scope=YOUTUBE_UPLOAD_SCOPE,
message=MISSING_CLIENT_SECRETS_MESSAGE)
I received this error because I still had the square brackets inside the client_id and client_secret. It should just be the string with no brackets.
If you are using Windows system, follow this steps:
Put your file (client_secrets.json) in the directory (C:) or (D:).
In your Python file define your variable like this:
CLIENT_SECRETS_FILE = "\client_secrets.json". Python will search the json file in the root C: or D: and will find it.
I had same problem with Google API for youtube and I solved it like that.

How to set the content type header in response for a particular file type in Pyramid web framework

I am using pyramid web framework to build a website. I keep getting this warning in chrome console:
Resource interpreted as Font but transferred with MIME type application/octet-stream: "http:static/images/fonts/font.woff".
How do I get rid of this warning message?
I have configured static files to be served using add_static_view
I can think of a way to do this by adding a subscriber function for responses that checks if the path ends in .woff and setting the response header to application/x-font-woff. But it does not look like a clean solution. Is there a way to tell Pyramid to do it through some setting.
Pyramid uses the standard mimetypes module to guess the mimetype based on the extension. It calls:
mimetypes.guess_type(path, strict=False)
The module looks in the Windows registry if on that platform, and in the following locations for mimetype lists:
knownfiles = [
"/etc/mime.types",
"/etc/httpd/mime.types", # Mac OS X
"/etc/httpd/conf/mime.types", # Apache
"/etc/apache/mime.types", # Apache 1
"/etc/apache2/mime.types", # Apache 2
"/usr/local/etc/httpd/conf/mime.types",
"/usr/local/lib/netscape/mime.types",
"/usr/local/etc/httpd/conf/mime.types", # Apache 1.2
"/usr/local/etc/mime.types", # Apache 1.3
]
You can either extend one of those files, or create your own file and add it to the module using the .init() function.
The file format is simple, just list the mimetype, then some whitespace, then a space-separated list of extensions:
application/x-font-woff woff
Simply add this following code where your Pyramid web app gets initialized.
import mimetypes
mimetypes.add_type('application/x-font-woff', '.woff')
For instance, I have added it in my webapp.py file, which gets called the first time the server gets hit with a request.

Installing a CGI Proxy on Tomcat

I want to setup a proxy for openlayers to use so I followed these steps:
Downloaded the proxy.cgi file from the OpenLayers web site:
http://trac.osgeo.org/openlayers/browser/trunk/openlayers/examples/proxy.cgi
Modify the proxy.cgi file to include my domain in the allowedHosts list:
allowedHosts = ['localhost:6901']
Copy the proxy.cgi file to the following folder:
$TOMCAT_PATH$/webapps/yourApp/WEB-INF/cgi/
Modify the file web.xml of your web app by adding the sections below. You find the file at
$TOMCAT_PATH$/webapps/yourApp/WEB-INF/web.xml
Comment: In case the web.xml file doesn’t exist for your webapp, just create it yourself or copy it from another webapp and modify it. (created!)
Comment: the “param-value” for the “executable” parameter has to contain the path to your Python installation. (it does!)
<servlet>
<servlet-name>cgi</servlet-name>
<servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>cgiPathPrefix</param-name>
<param-value>WEB-INF/cgi</param-value>
</init-param>
<init-param>
<param-name>executable</param-name>
<param-value>c:\python25\python.exe</param-value>
</init-param>
<init-param>
<param-name>passShellEnvironment</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>5</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cgi</servlet-name>
<url-pattern>/cgi-bin/*</url-pattern>
</servlet-mapping>
Modify the file context.xml of your web app by adding the element below. You find the file at $TOMCAT_PATH$/webapps/yourApp/META-INF/context.xml
Restart Tomcat
To use the proxy with OpenLayers, just include this single line into your code:
OpenLayers.ProxyHost = "/yourWebApp/cgi-bin/proxy.cgi?url=";
But when I try to use it like:
/webappname/cgi-bin/proxy.cgi?url=labs.metacarta.com
I get this error:
Some unexpected error occurred. Error text was: list index out of range
I think its related with os.environ["REQUEST_METHOD"] but I dont know how its related ..
You're asking for an environment variable that isn't defined.
You need to either catch and handle the exception or use os.environ.get:
try:
methodq = os.environ["REQUEST_METHOD"]
except KeyError:
methodq = "default value"
Or:
methodq = os.environ.get("REQUEST_METHOD", "default value")
You're submitting :
/webappname/cgi-bin/proxy.cgi?url=labs.metacarta.com
But the proxy.cgi script is trying to do this:
host = url.split("/")[2]
Try http://labs.metacarta.com for your url so proxy.cgi has some slashes to split on, or modify it to parse the url a little smarter.
The answer is: You don't install or use cgi proxy on Tomcat.
cgi is for apache server or IIS that are used as a front-end server. Tomcat may sit behind it. The configuration of Apache is detailed in: http://tomcat.apache.org/tomcat-6.0-doc/proxy-howto.html
Be warned that OpenLayers warns that its proxy.cgi is only an example and may not have good enough check to stop it from being exploited, i.e. it may run some malicious script.
If you are serving your OpenLayers client page on Tomcat alone and it contains layers from other GeoServer or Mapserver, you can use proxy servlet and specify it as:
OpenLayers.ProxyHost = "sevlet URL on the server that served this page";
http://wiki.apache.org/tomcat/ServletProxy
https://svn.openxdata.org/Experimental/openXmapper/trunk/gwt-openlayers-server/src/main/java/org/gwtopenmaps/openlayers/server/GwtOpenLayersProxyServlet.java
There are reverse proxy or rewrite sevlet solutions to it, too. Please Google on these.

Categories