You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

87 lines
2.5 KiB

  1. /*
  2. *
  3. * Copyright 2018 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. // The client demonstrates how to supply an OAuth2 token for every RPC.
  19. package main
  20. import (
  21. "context"
  22. "flag"
  23. "fmt"
  24. "log"
  25. "time"
  26. "golang.org/x/oauth2"
  27. "google.golang.org/grpc"
  28. "google.golang.org/grpc/credentials"
  29. "google.golang.org/grpc/credentials/oauth"
  30. ecpb "google.golang.org/grpc/examples/features/proto/echo"
  31. "google.golang.org/grpc/testdata"
  32. )
  33. var addr = flag.String("addr", "localhost:50051", "the address to connect to")
  34. func callUnaryEcho(client ecpb.EchoClient, message string) {
  35. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  36. defer cancel()
  37. resp, err := client.UnaryEcho(ctx, &ecpb.EchoRequest{Message: message})
  38. if err != nil {
  39. log.Fatalf("client.UnaryEcho(_) = _, %v: ", err)
  40. }
  41. fmt.Println("UnaryEcho: ", resp.Message)
  42. }
  43. func main() {
  44. flag.Parse()
  45. // Set up the credentials for the connection.
  46. perRPC := oauth.NewOauthAccess(fetchToken())
  47. creds, err := credentials.NewClientTLSFromFile(testdata.Path("ca.pem"), "x.test.youtube.com")
  48. if err != nil {
  49. log.Fatalf("failed to load credentials: %v", err)
  50. }
  51. opts := []grpc.DialOption{
  52. // In addition to the following grpc.DialOption, callers may also use
  53. // the grpc.CallOption grpc.PerRPCCredentials with the RPC invocation
  54. // itself.
  55. // See: https://godoc.org/google.golang.org/grpc#PerRPCCredentials
  56. grpc.WithPerRPCCredentials(perRPC),
  57. // oauth.NewOauthAccess requires the configuration of transport
  58. // credentials.
  59. grpc.WithTransportCredentials(creds),
  60. }
  61. conn, err := grpc.Dial(*addr, opts...)
  62. if err != nil {
  63. log.Fatalf("did not connect: %v", err)
  64. }
  65. defer conn.Close()
  66. rgc := ecpb.NewEchoClient(conn)
  67. callUnaryEcho(rgc, "hello world")
  68. }
  69. // fetchToken simulates a token lookup and omits the details of proper token
  70. // acquisition. For examples of how to acquire an OAuth2 token, see:
  71. // https://godoc.org/golang.org/x/oauth2
  72. func fetchToken() *oauth2.Token {
  73. return &oauth2.Token{
  74. AccessToken: "some-secret-token",
  75. }
  76. }