How to pass body with Google APIs Client Library? - python

I use Google APIs Client Library for Python to work with Fusion Tables API. importRows method here requires to provide the data in the body. How should I do it?
response = service.table().importRows(tableId=TABLE_ID, body='zzz,yyy').execute()
returns the error - Got an unexpected keyword argument "body".

There's a slight subtlety here -- the body of the request should be the Table resource, if you want to update it; the contents (in this case, the rows) should actually be passed as a media upload.
In the python client, this means you want to pass something in to the media_body argument, not body. You can't just pass a literal string -- you need to wrap the data in either a MediaFileUpload or MediaInMemoryUpload. (For the case here, you want the latter, but if you've got a file with rows on disk, you want the former.)

Related

Understanding protobuff protocol

I'm just doing some reverse engineering exercise and have ran across application/x-protobuff protocol..
I am currently sniffing network calls from redfin using mitmproxy. I see a endpoint for a result, however the response is unstructured JSON formatted data with content type application/x-protobuff After doing a bit of research, I found out that protobuff uses a schema to map the data internally, and I am assuming the schema also sits in the client somewhere, called .proto file.
SS
To validate my assumption on what that screenshot tells is that
I can see there is a response header called X-ProtoBuf-Schema is that the the location where the schma would be located, the same schema I can use to decrypt the response data? How would I go on about reading that data in a more structured manner?
I am able to make a request using requests to that endpoint, just gives me protobuffers.
PS: This is what the JSON format looks like
https://pastebin.com/LY51X9KZ
"and I am assuming the schema also sits in the client somewhere, called .proto file." - I wouldn't assume that at all; the client, once built, doesn't need the .proto - the generated code is used instead of any explicit schema. If a site is publishing a schema, it is probably a serialized FileDescriptorSet from google/protobuf/descriptor.proto, which contains the intent of the .proto, but as data.

Get Headers/Cookies from Bottle API request

I was making an API call to a Bottle service and was passing headers in the call using Python's request Library.
requests.get('http://localhost/API/call', headers={"cat":"tax"})
I wanted to get the custom headers passed in the function that gets called through the API call.
Using bottle.request.headers I get the following data:
Now, the custom header I passed is present in the environ dictionary with key/value 'HTTP_CAT':tax.
Same thing for cookies. Cookie data can be retrieved using bottle.request.cookies
How can I filter out only the custom header that I am passing in the request?
I'm not sure exactly what you mean by "filter," but the typical way to retrieve request headers from Bottle is with get_header:
cat_value = request.get_header('cat')
Bottle also has a specific API for retrieving individual cookies. Perhaps there's a good reason you're going down to the raw environ, but if not, then you should be using these built-in methods.
PS, you may also want to prefix your custom headers with "X-", e.g. X-Cat.

Ignoring a ProtoRPC message field via Cloud Endpoints

I've been working on an AppEngine-based project and I wanted to know if it's possible to ignore a ProtoRPC message field.
With the Java SDK, you can use #ApiResourceProperty to ignore a property (this means it's not contained within the response returned to the browser). However, I have not come across a way of doing this using the Python SDK.
Is there anything like this in the Python SDK?
Thanks, Adil
Nope, unfortunately not (at least not to my knowledge).
Two possible solutions depending on your use-case.
Set field values to None before returning the message in your method. That way they will be skipped/not included in the JSON response.
If your messages are hooked up to datastore models you can use the endpoints-proto-datastore library which allows you to use your ndb models directly in your API methods. Additionally it allows for request_fields and response_fields parameters in the method decorator which will limit the request or response to the specified subset of message/model fields. (internally it creates the necessary message classes for you)

How to get access to the URL segments after a # in Python Flask?

I'm trying to support OAuth2 login through Python Flask, so I want to handle a URL that looks like this:
http://myserver/loggedIn#accessToken=thisIsReallyImportant
but when I handle the callback it just seems to drop all the characters after the # in the URL, which contains the important Oauth access token. How do I get this info? It's not included in request.url
ETA: I can retrieve it in client-side javascript using window.location in Javascript, but then I'd have to pass it back to the server, which feels a little hokey but maybe Oauth2 is meant to be done that way?
From the RFC:
Fragment identifiers have a special role in information retrieval
systems as the primary form of client-side indirect referencing
[...]
the fragment identifier is not used in the scheme-specific
processing of a URI; instead, the fragment identifier is separated
from the rest of the URI prior to a dereference
As such, flask drops everything after the '#'. If you want to forward these to the server, you'll have to extract them on the client and pass them to the server via a query parameter or part of the URL path.
You are using the incorrect OAuth 2 grant type (implicit grant) for what you want to do. Implicit grant supplies the token in the fragment as you observed to be used by a javascript client. There is another type of grant, authorization code, which is similar but supplies it in the URI query which you can access from Flask.
You can tell the two apart from the the redirect URI you create for authorization, if it has response_code=code you are on the right track. You currently use response_code=token.
If you are using Facebook look at https://developers.facebook.com/docs/facebook-login/login-flow-for-web-no-jssdk/
For Google look at https://developers.google.com/accounts/docs/OAuth2WebServer
You might also be interested in https://flask-oauthlib.readthedocs.org/en/latest/ which can help you with OAuth.

python using post method for upload file

I have a problem with importing file to calendar over wcap protocol. In documentation http://docs.sun.com/source/816-6416-10/pr10WCAP.html#26125 is said that i should use POST method. I have trayed but with no positiv result. I have no idea hov to forced it to work with python
The wcap reference you provide contains example POST data.
Using the urllib2.urlopen() function with POST data is straightforward.
urllib2.urlopen(url[, data][, timeout])
Open the URL url, which can be either a string or a Request object.
data may be a string specifying additional data to send to the server, or None if no such data is needed. Currently HTTP requests are the only ones that use data; the HTTP request will be a POST instead of a GET when the data parameter is provided. data should be a buffer in the standard application/x-www-form-urlencoded format. The urllib.urlencode() function takes a mapping or sequence of 2-tuples and returns a string in this format.

Categories