zurche / beer-detector

An Android Beer Detector developed to test Google's Vision API
https://android.jlelse.eu/how-to-get-android-to-tell-if-your-beer-is-ok-yes-your-beer-a313b6c97a59
10 stars 1 forks source link

How to get API key? #1

Closed StanislavZakharov closed 7 years ago

StanislavZakharov commented 7 years ago

Smthing going wrong when put my API Key in code. The app crashes with error:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List LogoResponse.getResponses()' on a null object reference

One more question. In your article on Medium you say, that 'Now that I had the Vision API ready to use, I started testing it playing around encoding some images and getting some responses on what that images were' and give some screenshots. Where did yo test it? It is special program or what?

zurche commented 7 years ago

Hello there Stanislav, you certainly wont be able to use this app out of the box since like I also pointed out in the article Google charges you for each api request you make to its Vision API servers. So I checked in this repository with a "insert your api key here" place holder because otherwise anyone using the repository would be able to use my vision API and therefore charging my own credit card :)

Once the api key is place, this example works perfectly since its the only thing I removed. I suggest you create your own dashboard in google cloud here: https://cloud.google.com/vision/ setup your project and get a valid vision api key.

Once you get your key, simply add it in the retrofit service of my project: https://github.com/zurche/beer-detector/blob/master/app/src/main/java/com/zurche/beerclassifier/retrofit/service/VisionApiService.java

And remember not to share it too!

Regards!

StanislavZakharov commented 7 years ago

Yes, I understand that API KEY should be my own. I put there my own to try your project, but it gives me an error however. Maybe I did smthing wrong in Google Cloud...My API KEY looks like:

1069232484wfwf54-frvf3h0klfliut841c5aj1wjfwjpeuf0ase7ipa.apps.googleusercontent.com (I changed it to random to show an example)

I also enable billing and enable Vision API.

The problem is still occur. Do not know how to fix it.

zurche commented 7 years ago

Ok I see so, some things to check that may be producing the issue:

  1. Does the camera of the device you are testing works ok?
  2. Have you checked if your Google Cloud console is properly setup by sending a base64 encoded image using some HTTP request checker such as postman or ARC?

I you did both and they are both ok, whats left to check is some debugging in the IDE

StanislavZakharov commented 7 years ago
  1. The camera of the device is OK.
  2. I just check url which I send in postman and it tells me:

<!DOCTYPE html>

Error 404 (Not Found)!!1

404. That’s an error.

The requested URL /v1/images:annotate/106905242784-frvo6asvssdf1k4liut841c5aj1puf0asewgefwfw7ipa.apps.googleusercontent.com was not found on this server. That’s all we know. So, the requested URL cannot be found on the server...Do not know what to do with it. Also tried to use breakpoints when debbuging app in AS. I put breakpoint in OnResponse method and I receive Log that 'Got response succesfully'.

zurche commented 7 years ago

So the problem maybe its related to your Google Cloud configuration since I tested with the following url: Type: POST URL: https://vision.googleapis.com/v1/images:annotate?key=_MY_API_KEY_HERE_ RawPayload:

{
  "requests":[
    {
      "image":{
        "content":"YOUR_BASE_64_ENCODED_IMAGE_HERE"
      },
      "features":[
        {
          "type":"LOGO_DETECTION",
          "maxResults":1
        }
      ]
    }
  ]
}

That should return a 200OK with the prediction of the image, if that does not work, the app wont do it either.

StanislavZakharov commented 7 years ago

Yes, I understand that something wrong in Google Cloud. Maybe I do not take API Key correctly. Did you create API KEY as mentioned here? Or some else way?

zurche commented 7 years ago

I just followed Its own how-to's right here: https://cloud.google.com/vision/docs/common/auth

Hope that helps! And let me know if you have any issues with the app!