Как я могу исправить ошибку запроса Cloud Cloud API? - PullRequest
0 голосов
/ 23 марта 2020

Я занимаюсь разработкой Android приложения, которое показывает решение математического уравнения с использованием API google vision и wolfram alpha api в java, но я принимаю ошибку в том, что запрос api cloud vision api не выполнен. Проверьте подробности в журналах. Как я могу исправить эту ошибку , Мне нужна ваша помощь для моего дипломного проекта. Пожалуйста, помогите мне.

private void callCloudVision(final Bitmap bitmap) throws IOException {

    // Do the real work in an async task, because we need to use the network anyway
    new AsyncTask<Object, Void, String>() {
        @Override
        protected String doInBackground(Object... params) {
            try {
                Log.e("Line 255:", "This line has been executed");
                HttpTransport httpTransport = AndroidHttp.newCompatibleTransport();
                JsonFactory jsonFactory = GsonFactory.getDefaultInstance();


                VisionRequestInitializer requestInitializer =
                        new VisionRequestInitializer(CLOUD_VISION_API_KEY) {
                            /**
                             * We override this so we can inject important identifying fields into the HTTP
                             * headers. This enables use of a restricted cloud platform API key.
                             */
                            @Override
                            protected void initializeVisionRequest(VisionRequest<?> visionRequest)
                                    throws IOException {
                                super.initializeVisionRequest(visionRequest);

                                String packageName = getPackageName();
                                visionRequest.getRequestHeaders().set(ANDROID_PACKAGE_HEADER, packageName);

                                String sig = PackageManagerUtils.getSignature(getPackageManager(), packageName);

                                visionRequest.getRequestHeaders().set(ANDROID_CERT_HEADER, sig);
                            }
                        };

                Vision.Builder builder = new Vision.Builder(httpTransport, jsonFactory, null);
                builder.setVisionRequestInitializer(requestInitializer);

                Vision vision = builder.build();

                BatchAnnotateImagesRequest batchAnnotateImagesRequest =
                        new BatchAnnotateImagesRequest();
                batchAnnotateImagesRequest.setRequests(new ArrayList<AnnotateImageRequest>() {{
                    AnnotateImageRequest annotateImageRequest = new AnnotateImageRequest();

                    // Add the image
                    Image base64EncodedImage = new Image();
                    // Convert the bitmap to a JPEG
                    // Just in case it's a format that Android understands but Cloud Vision
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, byteArrayOutputStream);
                    byte[] imageBytes = byteArrayOutputStream.toByteArray();

                    // Base64 encode the JPEG
                    base64EncodedImage.encodeContent(imageBytes);
                    annotateImageRequest.setImage(base64EncodedImage);

                    // add the features we want
                    annotateImageRequest.setFeatures(new ArrayList<Feature>() {{
                        Feature textDetection = new Feature();
                        textDetection.setType("TEXT_DETECTION");
                        add(textDetection);
                    }});

                    // Add the list of one thing to the request
                    add(annotateImageRequest);
                }});

                Vision.Images.Annotate annotateRequest =
                        vision.images().annotate(batchAnnotateImagesRequest);
                // Due to a bug: requests to Vision API containing large images fail when GZipped.
                annotateRequest.setDisableGZipContent(true);

                BatchAnnotateImagesResponse response = annotateRequest.execute();
                return convertResponseToString(response);

            } catch (GoogleJsonResponseException e) {
            } catch (IOException e) {

            }
            return "Cloud Vision API request failed. Check logs for details.";
        }

Похоже, я где-то что-то упустил. Я принимаю эту ошибку в logcat.

  com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
    "code": 403,
    "errors": [
            {
               "domain": "global",
           "message": "This API method requires billing to be enabled. 
                 Please enable billing on project #285568034008 by visiting https://console.developers.google.com/billing/enable?project=285568034008 then retry.
                 If you enabled billing for this project recently, wait a few minutes for the action to propagate to our systems and retry.",
               "reason": "forbidden"
        }
    ],
"message": "This API method requires billing to be enabled. Please enable billing on project #285568034008 by visiting https://console.developers.google.com/billing/enable?project=285568034008 then retry. If you enabled billing for this project recently, wait a few minutes for the action to propagate to our systems and retry.",
"status": "PERMISSION_DENIED" 
}
...