Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The Application Default Credentials are not available #175

Closed
Raviraj20 opened this issue May 5, 2017 · 47 comments

Comments

@Raviraj20
Copy link

@Raviraj20 Raviraj20 commented May 5, 2017

Getting an similar error when using an .Net
Can someone help how to solve this error

Message":"An error has occurred.","ExceptionMessage":"The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.","ExceptionType":"System.InvalidOperationException","StackTrace":" at Google.Api.Gax.TaskExtensions.WaitWithUnwrappedExceptions(Task task)\r\n at Google.Api.Gax.Grpc.ChannelPool.GetChannel(ServiceEndpoint

@jskeet

This comment has been minimized.

Copy link
Collaborator

@jskeet jskeet commented May 5, 2017

We can't really help without more context:

  • Where are you running this code? (On GCP in some form, or not?)
  • Do you have the GOOGLE_APPLICATION_CREDENTIALS environment variable assigned a value?
  • If not, have you logged in with gcloud auth application-default login?

Basically, we need to know how you expect to authenticate in order to find out why it's not working.

@Raviraj20

This comment has been minimized.

Copy link
Author

@Raviraj20 Raviraj20 commented May 8, 2017

@jskeet
thanks for replying
i have set GOOGLE_APPLICATION_CREDENTIALS environment variable assigned
and done gcloud auth application-default login

but
GoogleCredential credential = await GoogleCredential.GetApplicationDefaultAsync();
this line gives compilation error when i tye this

@jskeet

This comment has been minimized.

Copy link
Collaborator

@jskeet jskeet commented May 8, 2017

Well what's the compilation error, and is this within an async method? (If not, I'd expect it to fail for that reason.)

Again, please give us details of the context in which the code is running - there isn't really enough information for us to help you at the moment.

@Raviraj20

This comment has been minimized.

Copy link
Author

@Raviraj20 Raviraj20 commented May 8, 2017

@jskeet its showing me this error

image

@jskeet

This comment has been minimized.

Copy link
Collaborator

@jskeet jskeet commented May 8, 2017

Right, yes - that's got nothing to do with the library, and everything to do with how async/await works. You can't use await outside an asynchronous method or anonymous function.

Now it's not clear why you're calling this at all - we still don't know which API you're trying to use, or how. Given your screenshot, presumably you're writing a WebAPI application, but we don't know which version of the framework you're targeting, or the way you're running your code when it fails. (Does it work when debugging locally, but fail on a server, for example?)

@Raviraj20

This comment has been minimized.

Copy link
Author

@Raviraj20 Raviraj20 commented May 8, 2017

yup I m writing an web Api
which will internaly called the google could api
i m using a .net framework 4.5

frist i have make a simple program
image

its woked for me but when i am hosting my Api and calling it from different PC then its gives error

but if i m using it in a local maching with out hosting its work fine

when i am hosting my Api and calling it from different PC then its gives error
and saying
image

@Raviraj20

This comment has been minimized.

Copy link
Author

@Raviraj20 Raviraj20 commented May 8, 2017

@jskeet
yess it work when debugging locally, but fail on a server, for example?)
when i use this code
image

@jskeet

This comment has been minimized.

Copy link
Collaborator

@jskeet jskeet commented May 8, 2017

It would be helpful if you'd post code - and stack traces - as text rather than images. It's a lot easier to read that way.

It sounds like you just need to set GOOGLE_APPLICATION_CREDENTIALS on your server, in the system environment variables, and make sure the service account JSON file is accessible to the hosting process.

@jskeet jskeet closed this May 8, 2017
@jskeet jskeet reopened this May 8, 2017
@Raviraj20

This comment has been minimized.

Copy link
Author

@Raviraj20 Raviraj20 commented May 8, 2017

yup I have set GOOGLE_APPLICATION_CREDENTIALS on server, in the system environment variables, and the service account JSON

here my code
its work fine when i m using it in a local maching its work fine

when i am hosting my Api and calling it from different PC then its gives error
and saying
public class PansLogic1
{
// string[] words;
string img = " ";
int total;

    public void PanDetails()
    {
   
        var client = ImageAnnotatorClient.Create();
        //// Load the image file into memory
        var image = Image.FromFile("12.jpg");
        //// Performs label detection on the image file
        var response = client.DetectText(image);
        string s = response[0].Description;
          }
 }
@jskeet

This comment has been minimized.

Copy link
Collaborator

@jskeet jskeet commented May 8, 2017

Could you just check that you can fetch the environment variable within your code first? My guess is that it's really not set - otherwise I would expect you to get a different error.

@Raviraj20

This comment has been minimized.

Copy link
Author

@Raviraj20 Raviraj20 commented May 8, 2017

ya i have set GOOGLE_APPLICATION_CREDENTIALS
with credential.json in classpath

@Raviraj20

This comment has been minimized.

Copy link
Author

@Raviraj20 Raviraj20 commented May 8, 2017

how can i fetch the environment variable within your code in my project

@Raviraj20

This comment has been minimized.

Copy link
Author

@Raviraj20 Raviraj20 commented May 8, 2017

how can i fetch the environment variable within code in my project

@jskeet

This comment has been minimized.

Copy link
Collaborator

@jskeet jskeet commented May 8, 2017

Just call string value = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS);

Log that, or return it as a value just in a test API...

@Raviraj20

This comment has been minimized.

Copy link
Author

@Raviraj20 Raviraj20 commented May 8, 2017

yup i m able to access that
fetch the environment variable within my code
image

@jskeet

This comment has been minimized.

Copy link
Collaborator

@jskeet jskeet commented May 8, 2017

But that's showing a debugger, so it's presumably not on the machine where things aren't working. Return that in another API method, and try it in the same environment where the credentials fail.

@Raviraj20

This comment has been minimized.

Copy link
Author

@Raviraj20 Raviraj20 commented May 8, 2017

ok
in my code
i am using a string variable value which stores the path of creadential.json file

so where should i user this value variable in my code

public class PansLogic1
{

    public void PanDetails()
    {
        string value = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");**

        PanGS p = new PanGS();
        var client = ImageAnnotatorClient.Create();
        //// Load the image file into memory
        var image = Image.FromFile("12.jpg");
        //// Performs label detection on the image file
        var response = client.DetectText(image);
        string s = response[0].Description;

}

@Raviraj20

This comment has been minimized.

Copy link
Author

@Raviraj20 Raviraj20 commented May 8, 2017

now i m getting a different error

{"Message":"An error has occurred.","ExceptionMessage":"Could not find file 'c:\windows\system32\inetsrv\12.jpg'.","ExceptionType":"System.IO.FileNotFoundException","StackTrace":" at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)\r\n at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)\r\n at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)\r\n at System.IO.File.InternalReadAllBytes(String path, Boolean checkHost)\r\n at Google.Cloud.Vision.V1.Image.FromFile(String path)\r\n at

@jskeet

This comment has been minimized.

Copy link
Collaborator

@jskeet jskeet commented May 8, 2017

Remove all the Image API code. Just make your WebAPI return the value of the environment variable. That's all we're trying to do at the moment. Please focus on one thing at a time.

@Raviraj20

This comment has been minimized.

Copy link
Author

@Raviraj20 Raviraj20 commented May 8, 2017

@jskeet
just say me one thing that
WebAPI return the value of the environment variable and we are storing it in value
so where to use value again

@jskeet

This comment has been minimized.

Copy link
Collaborator

@jskeet jskeet commented May 8, 2017

You haven't shown a WebAPI method returning the environment variable. You've shown your code calling Environment.GetEnvironmentVariable, but you haven't shown a WebMethod returning that. Once you've got one, just hit the WebAPI (running on the server with problems) with a browser, so you can see what value is returned.

@Raviraj20

This comment has been minimized.

Copy link
Author

@Raviraj20 Raviraj20 commented May 8, 2017

ok let me try this

@Raviraj20

This comment has been minimized.

Copy link
Author

@Raviraj20 Raviraj20 commented May 9, 2017

@jskeet
thanks
alot final its get slove

@jskeet

This comment has been minimized.

Copy link
Collaborator

@jskeet jskeet commented May 9, 2017

Sorry, I'm afraid I don't understand that comment...

@Raviraj20

This comment has been minimized.

Copy link
Author

@Raviraj20 Raviraj20 commented May 9, 2017

@jskeet
I m saying
finaly issuse get slove with the help of your

so Thank you

@jskeet

This comment has been minimized.

Copy link
Collaborator

@jskeet jskeet commented May 9, 2017

Ah, you mean solved? Great - closing the issue then.

@jskeet jskeet closed this May 9, 2017
@shivp

This comment has been minimized.

Copy link

@shivp shivp commented May 27, 2018

I am facing the same issue. I have set the environment variable on the production server. I have checked whether the value is accessible using by running an windows exe that fetches the environment variable value, on the production server. I have give access to "Everyone" group on the folder where the credentials JSON file resides. It worked on our network server by setting the permissions. But it does not work on the production server. The error description isn't very helpful. The same error message was showing when there was a mismatch in the versions of the Newtonsoft.Json dll. I have added the assembly binding redirects in web.config file. @Raviraj20, can you please elaborate on what solution you applied ?

@farhatkhan

This comment has been minimized.

Copy link

@farhatkhan farhatkhan commented Jul 11, 2018

@Raviraj20

How can you set the environmental path for google credentials?
I am facing the same issue. Please tell me the complete code.

Thanks

@jskeet

This comment has been minimized.

Copy link
Collaborator

@jskeet jskeet commented Jul 11, 2018

@farhatkhan: You wouldn't normally set the environment variable with code - you'd do it however you set up the environment variable elsewhere. Without any more information on where you're running our code or what you've tried, there's not a lot more we can say.

@farhatkhan

This comment has been minimized.

Copy link

@farhatkhan farhatkhan commented Jul 11, 2018

@jskeet
I am working in visual studio 2015 console application. Please guide me how to set environment path. I am facing the "The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information."

@jskeet

This comment has been minimized.

Copy link
Collaborator

@jskeet jskeet commented Jul 11, 2018

@farhatkhan: So have you set the GOOGLE_APPLICATION_CREDENTIALS environment variable to refer to a file containing service account credentials? How have you done so, and how are you running the code? Note that if you set the environment variable system-wide after launching VS, that may well not "notice" the change, but restarting VS should help.

@farhatkhan

This comment has been minimized.

Copy link

@farhatkhan farhatkhan commented Jul 11, 2018

I don't know where set the environmental path. Can you guide me step by step?

@jskeet

This comment has been minimized.

Copy link
Collaborator

@jskeet jskeet commented Jul 11, 2018

See https://cloud.google.com/docs/authentication/getting-started - for how to set it in a particular console session, or from the Microsoft documentation

You can also view or modify the environment variables by selecting System from the Control Panel, selecting Advanced system settings, and clicking Environment Variables.

(Or just press the Windows key and type "environment", then select either "Edit the system environment variables" or "Edit the environment variables for your account".)

@farhatkhan

This comment has been minimized.

Copy link

@farhatkhan farhatkhan commented Jul 11, 2018

I have set environmental path : control panel-> environment->advanced->Environmental Variable->New->Browse json file and set it name. then press ok

Now how to set in c#?

@jskeet

This comment has been minimized.

Copy link
Collaborator

@jskeet jskeet commented Jul 11, 2018

@farhatkhan: You don't set it in C#. The library will pick it up automatically.

If you've already set the variable via control panel, and restarted Visual Studio since setting it, it should already work. If you're still getting this error, then one simple way of debugging it is to print out the environment variable value:

string credentialPath = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");
Console.WriteLine(credentialPath);

Check the value that prints out. If it's not printing out the filename you expect, double check control panel.

@farhatkhan

This comment has been minimized.

Copy link

@farhatkhan farhatkhan commented Jul 11, 2018

string credentialPath = Environment.GetEnvironmentVariable("googlecloudvision");
Console.WriteLine(credentialPath);
C:\Users\Administrator\Desktop\OCRTestImage\My Project-d424b41c7b5b.json
it send me proper path. but face again same issue on var client = ImageAnnotatorClient.Create();
The Application Default Credentials are not available. They are available if running in Google Compute Engine.

@jskeet

This comment has been minimized.

Copy link
Collaborator

@jskeet jskeet commented Jul 11, 2018

Why are you asking for an environment variable called googlecloudvision? The environment variable is meant to be GOOGLE_APPLICATION_CREDENTIALS, as per the error message. If that's not the environment variable you've set, that's the problem.

@farhatkhan

This comment has been minimized.

Copy link

@farhatkhan farhatkhan commented Jul 11, 2018

Now error comes at :var response = client.DetectText(image);
Additional information: Status(StatusCode=PermissionDenied, Detail="This API method requires billing to be enabled. Please enable billing on project #1077400448068 by visiting

@jskeet

This comment has been minimized.

Copy link
Collaborator

@jskeet jskeet commented Jul 11, 2018

Okay, so that sounds like a very different problem - that perhaps you haven't enabled billing. I suggest you look into it, and raise a new issue if necessary.

@kazmi1215

This comment has been minimized.

Copy link

@kazmi1215 kazmi1215 commented Oct 8, 2018

Hi,

I am a beginner in this field. I just want to read text from a picture but its giving me error. What should I do? I will really appreciate the help. Thank you
code
error

@jskeet

This comment has been minimized.

Copy link
Collaborator

@jskeet jskeet commented Oct 8, 2018

Well it sounds like you'd already got past that in your previous comment - you were making a call to the API, but you didn't have a billing account associated with your Cloud project. So go back to whatever you were doing before, but after fixing the project.

Anything I'd write in very general terms would be the same as is written at https://cloud.google.com/docs/authentication/getting-started. Could you explain what you've done to try to follow those instructions? Have you created and downloaded a service file? Have you set the environment variable? (If you want to debug via VS, the easiest approach is to set it as an environment variable for your user at all times, in the Windows environment variable editor.)

@kazmi1215

This comment has been minimized.

Copy link

@kazmi1215 kazmi1215 commented Oct 8, 2018

Thank you so much for the quick response. I have downloaded the JSON file in my system. Now what is next step? Where I can setup my environment variable?
--> (By reading from the link) Where should I copy this line of code in my program?
GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads[FILE_NAME].json"

Thank you

@jskeet

This comment has been minimized.

Copy link
Collaborator

@jskeet jskeet commented Oct 8, 2018

It's not a line of code. You could:

  • Call Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "..."); within your code, before doing anything else
  • Use set GOOGLE_APPLICATION_CREDENTIALS=[PATH] on the command line, which would only set it for that command session
  • Use the Windows dialog to change them for your Windows user: press the Windows key, type "environment" and select "Edit environment variables for your account"
@kazmi1215

This comment has been minimized.

Copy link

@kazmi1215 kazmi1215 commented Oct 8, 2018

Thank you so much. I solved it. Could you please tell me if I could limit the number of results? Like I am getting 10 12 results for every picture. Could I limit it to one?

Thank you

@jskeet

This comment has been minimized.

Copy link
Collaborator

@jskeet jskeet commented Oct 8, 2018

To do that, you'd have to create the AnnotateImageRequest request yourself, and call client.AnnotateImage with that.

Something like (untested):

var request = new AnnotateImageRequest
{
    Image = Image.FromFile(...),
    Features =
    {
        new Feature { MaxResults = 1, Type = Type.Feature.Types.Type.LabelDetection }
    }
};
var response = client.AnnotateImage(request);
var labels = response.LabelAnnotations;
// Use labels

But you may find it simplest to just ignore all the results after the first.

I'm going to close this issue now as we're quite a long way from the original problem, which has been resolved.

@maheshdeshmukh22

This comment has been minimized.

Copy link

@maheshdeshmukh22 maheshdeshmukh22 commented May 8, 2019

if you are referring in java code, then you can add env variable in eclipse.
go to class contains main method -> run configurations -> Environment -> create new var and assign file path

@jskeet

This comment has been minimized.

Copy link
Collaborator

@jskeet jskeet commented May 8, 2019

@maheshdeshmukh22: No, this is .NET. But I believe the problem is resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants
You can’t perform that action at this time.