0

I am trying to connect to dialogflow CX through the golang client, but I keep getting the error "failed to get discovery job for dialogflow.googleapis.com:443". This is my code:

import (
    dialogflow "cloud.google.com/go/dialogflow/cx/apiv3"
    "cloud.google.com/go/dialogflow/cx/apiv3/cxpb"
    "golang.org/x/oauth2/google"
    "google.golang.org/api/option"
)
....


func detectIntentText(
    agentID string,
    location string,
    sessionID string,
    text string,
    languageCode string) ([]*cxpb.ResponseMessage, error) {

    // This is my service account key
    credentials = "{"type": "service_account", "project_id"......"universe_domain", "googleapis.com"}"
    jwtConfig, err := google.JWTConfigFromJSON(
        []byte(credentials),
        "https://www.googleapis.com/auth/dialogflow"
    )
    if err != nil {
        return nil, err
    }
    sessionClient, err := dialogflow.NewSessionsClient(ctx, option.WithTokenSource(jwtConfig.TokenSource(ctx)))

    if err != nil {
        return nil, err
    }
...

And this is the error I get upon calling NewSessionsClient:

level="error" msg="finished unary call with code Internal" error="rpc error: code = 
Internal desc = failed to build resolver: failed to get discovery job for 
dialogflow.googleapis.com:443: discovery: invalid addr \"dialogflow.googleapis.com:443\", 
invalid endpoint \"443\"" grpc.code="Internal" grpc.context.UUID="7643ccb6-60b7-4468-
82f4-4983ab646886" 

What am I doing wrong?

sfendell
  • 5,415
  • 8
  • 25
  • 26
  • 2
    I recreated your code and it works for me. It's difficult to provide guidance. Your code as-is is both incomplete and incorrect. What OS are you using? Can the host on which you're running the code access `dialogflow.googleapis.com`? Ideally `grpcurl dialogflow.googleapis.com:443 list` or equivalent. – DazWilkin May 25 '23 at 01:43
  • 2
    It doesn't resolve your issue but you can simplify the code using Google's [Application Default Credentials](https://cloud.google.com/docs/authentication/application-default-credentials). Among other things, this would save hard-coding the JSON key into your binary but significantly simplifies the code, you can `sessionClient, err := dialogflow.NewSessionsClient(ctx)` – DazWilkin May 25 '23 at 01:44
  • Thanks for the reply, I was missing the `context` initialization above. The code is a snippet out of a larger file, in reality I'm not hardcoding the secret into my JSON but reading it from a secure file on my prod cluster. My issue is that my code's actually running on a kubernetes cluster. It's encouraging that it works for you, it lets me know I'm missing something in permissions or setup somewhere instead of code. – sfendell May 26 '23 at 00:21
  • Aha! Have you tried running the code out-of-cluster/locally? Have you tried running a Pod with a shell on your cluster and attempting to open a socket against `*.googleapis.com`? I suspect it would work locally and that your GKE cluster is restricting egress/public traffic. – DazWilkin May 26 '23 at 00:54

0 Answers0