Do Google Drive Realtime APIs support partial response? - google-drive-realtime-api

I'm using the Java Google Client Library to access a Realtime API method (namely, the "get" one). It seems to me that, unlike the vast majority of the other Google APIs, Realtime API methods do not support partial responses. I tried with:
HttpResponse response = drive.realtime().get(fileId).setFields("revision").executeMedia();
String responseString = CharStreams.toString(new InputStreamReader(response.getContent()));
responseString contains the whole response, while I was expecting a (much smaller)
{
"revision":3240
}
Has anyone experienced that?

Related

using /upload urls with Google API client

Consider this:
I am fetching rawbody messages by this call:
service.users().messages().get(userId='me', format='raw', id=msgid)
Then I am pushing rawbody messages by this Call:
service.users().messages().insert(userId='me', body=message)
Now when the mails contain attachments bigger 5MB, I encounter 413 "Request Entity Too Large.", and I can't push mails.
GMail API messages.insert Documentation advices to use
POST https://www.googleapis.com/upload/gmail/v1/users/userId/messages
instead of
POST https://www.googleapis.com/gmail/v1/users/userId/messages.
But Google API Client doesn't seem to have any documentation about how to call the above Url, and it keeps getting back to latter url.
How Can send post requests to first url(with /upload) with Google Api Client rather than its default?
How to use /upload url and set uploadType=multipart with Google APi Client?
Yeah, this was totally unclear from the documentation for the Google Python API client, but I found the solution in this other answer. It turns out that you use the same method (users().messages().insert()) but you pass media_body instead of body['raw']. Something like this should work:
from io import BytesIO
from base64 import urlsafe_b64decode
import googleapiclient.http
b = BytesIO()
message_bytes = urlsafe_b64decode(fetched_message['raw'])
b.write(message_bytes)
media_body = googleapiclient.http.MediaIoBaseUpload(b, mimetype='message/rfc822')
service.users().messages().insert(userId='me', media_body=media_body).execute()
I haven't tried using uploadType=multipart, but maybe you can figure it out from this documentation page and looking at the contents of the googleapiclient.http module.

Is apache commons like HttpClient library equivalent supported in Apex Code on Salesforce's Force.com Platform?

I am trying to perform an external Http REST API callout using Apex Class from within my Salesforce Development Organization.
I was wondering if there is support for an equivalent HttpClient library like that of Apache Commons' HttpClient. Is there one?
From the documentation I realize that one way of doing it would be use the System.Http class to perform the request. Refer here for :Salesforce's System Classes and System.Http.
Example:
public class HttpCalloutSample {
// Pass in the endpoint to be used using the string url
public String getContent(String url) {
// Instantiate a new http object
Http h = new Http();
// Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod('GET');
// Send the request, and return a response
HttpResponse res = h.send(req);
return res.getBody();
}
}
The reason I am asking this question is because I remember running across a Apex Code Snippet from a tutorial that used such an API. I cant seem to find it now.
P.S: I understand the the Apex Code is a different language and Java libraries like the HttpClient do not run on the Salesforce platform. And there may not be direct language level way to integrate them both, unless there is a Apex Code binding to the library.
The System.HTTP class and friends is the only way to make HTTP requests from ApexCode. As you say apex is not java and you can't run random java libraries in apex.

Cloud Endpoints in Java on GAE (not javascript)

Although I do think Cloud Endpoints are quite nifty, it would be great if I could use them directly in my GWT application in Java code, rather than writing masses of JSNI. Is this possible? I cannot find a way.
In other words, I would like to NOT use the Javascript Endpoints client, but all the endpoint methods using Java inside GWT.
I think you could use a simple request using Java's built-in HttpURLConnection object. Something like this:
//The URL would be the one you see when you execute the method from APIs Explorer...
String stringURL = "https://YOUR_APP_ID.appspot.com/_ah/api/API_NAME/VERSION/METHOD_PATH?param1=xxx&param2=yyy";
URL url = new URL(stringURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
And then you could parse the response, using Google's Gson library for example (but that would be another question...).
Note: I've never tried this, but I understand it should be working... If you eventually try it, please comment...

Cloud Endpoints HTTP Cookies

I am implementing Cloud Endpoints with a Python app that uses custom authentication (GAE Sessions) instead of Google Accounts. I need to authenticate the requests coming from the Javascript client, so I would like to have access to the cookie information.
Reading this other question leads me to believe that it is possible, but perhaps not documented. I'm not familiar with the Java side of App Engine, so I'm not quite sure how to translate that snippet into Python. Here is an example of one of my methods:
class EndpointsAPI(remote.Service):
#endpoints.method(Query_In, Donations_Out, path='get/donations',
http_method='GET', name='get.donations')
def get_donations(self, req):
#Authenticate request via cookie
where Query_In and Donations_Out are both ProtoRPC messages (messages.Message). The parameter req in the function is just an instance of Query_In and I didn't find any properties related to HTTP data, however I could be wrong.
First, I would encourage you to try to use OAuth 2.0 from your client as is done in the Tic Tac Toe sample.
Cookies are sent to the server in the Cookie Header and these values are typically set in the WSGI environment with the keys 'HTTP_...' where ... corresponds to the header name:
http = {key: value for key, value in os.environ.iteritems()
if key.lower().startswith('http')}
For cookies, os.getenv('HTTP_COOKIE') will give you the header value you seek. Unfortunately, this doesn't get passed along through Google's API Infrastructure by default.
UPDATE: This has been enabled for Python applications as of version 1.8.0. To send cookies through, specify the following:
from google.appengine.ext.endpoints import api_config
AUTH_CONFIG = api_config.ApiAuth(allow_cookie_auth=True)
#endpoints.api(name='myapi', version='v1', auth=AUTH_CONFIG, ...)
class MyApi(remote.service):
...
This is a (not necessarily comprehensive list) of headers that make it through:
HTTP_AUTHORIZATION
HTTP_REFERER
HTTP_X_APPENGINE_COUNTRY
HTTP_X_APPENGINE_CITYLATLONG
HTTP_ORIGIN
HTTP_ACCEPT_CHARSET
HTTP_ORIGINALMETHOD
HTTP_X_APPENGINE_REGION
HTTP_X_ORIGIN
HTTP_X_REFERER
HTTP_X_JAVASCRIPT_USER_AGENT
HTTP_METHOD
HTTP_HOST
HTTP_CONTENT_TYPE
HTTP_CONTENT_LENGTH
HTTP_X_APPENGINE_PEER
HTTP_ACCEPT
HTTP_USER_AGENT
HTTP_X_APPENGINE_CITY
HTTP_X_CLIENTDETAILS
HTTP_ACCEPT_LANGUAGE
For the Java people who land here. You need to add the following annotation in order to use cookies in endpoints:
#Api(auth = #ApiAuth(allowCookieAuth = AnnotationBoolean.TRUE))
source
(Without that it will work on the local dev server but not on the real GAE instance.)

Get Google SpreadsheetService using OAuth

I'm developing GWTP project, and below scenarios are tested successfully in my local development mode :
Get authenticated and authorized by OpenID and OAuth
Save GoogleOAuthParameters object into HttpSession.
Another action handler reuses the GoogleOAuthParameters stored in session to get SpreadsheetService object.
Use SpreadsheetService to manipulate spread sheets in GDoc.
However, when being deployed to App Engine, nothing can be read from GDoc, and no error/warning also, and returned list is always empty.
spreadsheetService = new SpreadsheetService("test");
GoogleOAuthParameters oauthParameters = (GoogleOAuthParameters)sessionProvider.get().getAttribute(HttpSessionProvider.PARAM_OAUTH_PARAMETERS);
spreadsheetService.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer());
oauthParameters.setScope(SCOPE_SPREADSHEET);
If I clearly use username/password as below when initializing SpreadsheetService, I can retrieve data from GDoc.
SpreadsheetService sService = new SpreadsheetService("test");
sService.setUserCredentials("username", "password");
I'm using App Engine SDK 1.6.6, and gdata-spreadsheet-3.0.
Please advise whether anything I did wrongly.
Thanks!
The documentation is for .Net, not Java. If you want to develop in Java, I found a solution here: Sharing authentication/token between Android Google Client API and SpreadSheet API 3.0
String token = GoogleAuthUtil.getToken(
this,
this.accountName,
this.scopes);
SpreadsheetService service = new SpreadsheetService("my-service-name");
service.setProtocolVersion(SpreadsheetService.Versions.V3);
service.setHeader("Authorization", "Bearer " + token);
Note that this does not work for setting the token and results in a 401 for me:
// BAD CODE
service.setUserToken(token);
// BAD CODE
The documentation has complete samples in Java showing how to perform authentication with all supported mechanisms. The recommendation is to use OAuth 2.0 which is explained at:
https://developers.google.com/google-apps/spreadsheets/#performing_oauth_20

Resources