Archive for the ‘Projects’ Category

Google API SDK OAuth2 Authentication “Invalid_Grant”

After hours of searching and not finding any information on the topic, I wanted to prevent a headache for the next guy who has to code against Google’s user directory API.

If you’ve had a solution working for a while, and all of a sudden your logs start to be filled with errors like this:

Google.Apis.Auth.OAuth2.Responses.TokenResponseException: Error:”invalid_grant”, Description:””, Uri:””
at Google.Apis.Requests.ClientServiceRequest`1.Execute()

The solution is as simple as checking the time is synchronized to match what Google has. If your server has a time that is even 1 minute in the future off, Google will send an exception of “Invalid_Grant” due to a request claiming to come from the future.

*Edit*: Found info on Google here.

 

XmlTransformer – Simplified XML / XSL Transformation in C#

XmlTransformer.cs

After a few days of trying to figure out how to manipulate XML with XSLT stylesheets in C#, I discovered it was slightly difficult. I personally don’t like difficult things remaining difficult, and promptly work on attempting to make them… less difficult.

If anybody wants to know how to apply XSLT to XML – I have included a class that is quite simple to use. Note though that it has only been used for my own purposes, and thus only tested for those purposes. If you do find bugs, or have xml/xslt that it does not work for, please send me examples of failures (if you are allowed) and I would be happy to fix any bugs.

 

If you have an XML document with processing instructions embedded:

1
< ?xml-stylesheet type="text/xsl" href="http://pathtoxslsheethere" ?>

Then usage is extremely simple:

1
2
3
4
5
6
 // optionally you can use XmlDocument:  XmlDocument doc = new XmlDocument();  and then load up the document
 XmlTransformer xmlToTransform = new XmlTransformer(doc,
      XsltAllow.DocumentFunction | XsltAllow.DtdProcessing | XsltAllow.ExternalResources);    
 //You can also explicitly set the settings in the properties of XmlTransformer
 string theTransformedDocument= xmlToTransform .ApplyTransformation();  
//Alternatively, you can pass in XSL or a URI to XSL or an XmlDocument object that has loaded an XSLT stylesheet.

If you do not have an XML document with processing instructions, the only part of the usage that changes is that you must add in a link/string/xmldocument pointing to or containing your XSLT to apply in the ApplyTransformation(); function.

Enjoy.

An HttpRequest and HttpResponse library for BB OS5+

A simple thing that is seen all over stack overflow with constant questions is how to do an http request. I’ll admit this library probably is not perfect, but it is quite in depth and you should be able to find anything you need inside of it. If you want to use it with the classes included in this post and this post download the extra package. If you do not, you’ll need to remove the references to the Logger class from the HttpRequest library.

HttpRequest – HttpRequest BlackBerry library

Javadoc – Javadoc for the library

Extra – extra package for Logger and ObservedThread HttpRequest implementation

 

Below is the usage for downloading a simple image file:

1
2
3
4
5
6
EncodedImage image;
HttpRequest request = new HttpRequest(_imageUrl);
HttpResponse response = request.getResponse();
if (response.getMimeType().equalsIgnoreCase("image/jpeg")){
	image = EncodedImage.createEncodedImage(response.getResponseBytes(), 0, -1);
}

Note that this class will only attempt connections that are available and have enough signal strength. You can use the “Transport” class to check different transports or get a list of available (service book exists and signal strength is high enough, i.e.  getFunctionalTransports() ) connections.  For example “Transport.isWifiSupported()” obviously tells you whether wifi is supported.

 

Return top

INFORMATION

I do not adhere to a schedule. I post when I have something to say. I'm a programmer who in my spare time enjoys contemplating the meaning of life, the universe and, everything. So there will be code as well as random little stories, essays and, musing about whatever interests me at the moment.