Google Search: How to construct a reverse image search URL (take 2)

1.7k Views Asked by At

I have a need to use the Google reverse image search pragmatically, the thing is that they deprecated the version of the API that allowed to search images with images. In my scenario i can't really use the search image by text.

So i decided to do a "quick" prototype by progragmatically posting the image to http://images.google.com/searchbyimage/upload first i did it manually and sniffed with fiddler to see what was being posted, I have also found this useful stackoverflow post. ( mikerobi's take on it)

Long story short the reason i need to post this to http://images.google.com/searchbyimage/upload is because i need to retrieve the tbs:sbi=xxx returned by the post; this "image fingerprint/encoded image" (which is not a base64 encoded image) is a parameter for a later post that will need to be done to retrieve the image results (again programatically)

the parameters posted progragmatically are almost if not identical to the one done when using the browser my sample code is the following; the result does give me a tbs:sbi=xxx but its not the correct one, what I'm I doing wrong?

here is a sample of my code

    [TestMethod]
    public void PostImageToGoogleAsBytes()
    {
        const string googleImageSearchUrl = @"http://images.google.com/searchbyimage/upload. ";
        const string imageToUpload = @"C:\Development\ReverseImageSearch\Tests\TestFiles\Images\sampleImage.jpg";
        HttpContent bytesContent = new ByteArrayContent(File.ReadAllBytes(imageToUpload));
        bytesContent.Headers.Add("Content-Type","image/jpeg");
        bytesContent.Headers.Add("filename", "sampleImage.jpg");

        using (var client = new HttpClient())
        {
            using (var formData = new MultipartFormDataContent())
            {
                formData.Add(new StringContent(String.Empty),"image_url");
                formData.Add(bytesContent, "encoded_image");
                formData.Add(new StringContent(String.Empty), "fileName");
                formData.Add(new StringContent(String.Empty), "image_content");
                var response = client.PostAsync(googleImageSearchUrl, formData).Result;
                Assert.IsTrue(response.IsSuccessStatusCode);
            }
        }
    }
0

There are 0 best solutions below