How to call google cloud identity APIs from an application

huangapple go评论83阅读模式
英文:

How to call google cloud identity APIs from an application

问题

我正在尝试从我的基于Go的应用程序中调用Google Cloud的组成员资格API。我查看了Go的文档这里的文档,但我不确定如何在Go中编写一个简单的代码来实现这一点。

这是我尝试编写的代码:

package main

import (
	"context"
	"google.golang.org/api/cloudidentity/v1beta1"
)

func main() {
	ctx := context.Background()
	cloudidentityService, err := cloudidentity.NewService(ctx)
	res, err := cloudidentity.Groups.Memberships.Lookup('placeholder-value', 'memberKey.namespace')
}

运行时,我收到错误消息"cannot refer to unexported name cloudidentity.groups"。

我对Google Cloud和Go都是新手。我的任务是查找用户是否是Google Cloud中给定组的一部分。


<details>
<summary>英文:</summary>

I am trying to call google cloud [groups memberships apis][1] from my go based application. I looked at the documentation in go [document here][2] but I am not sure how to write a simple code to do that in go. 

here is what I tried to write:

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

    package main

    import (
    	&quot;context&quot;
    	&quot;google.golang.org/api/cloudidentity/v1beta1&quot;
      )
      
     func main() {  
    	ctx := context.Background()
    	cloudidentityService, err := cloudidentity.NewService(ctx)
    	res, err := cloudidentity.groups.memberships.lookup(&#39;placeholder-value&#39;, &#39;memberKey.namespace&#39;)
     }

&lt;!-- end snippet --&gt;

On running this i get error &quot;cannot refer to unexported name cloudidentity.groups&quot;

I am new to Google cloud as well as golang. My task is to find if the user is part of a given group in google cloud.
  [1]: https://cloud.google.com/identity/docs/reference/rest/v1beta1/groups.memberships
  [2]: https://pkg.go.dev/google.golang.org/api/cloudidentity/v1beta1#GroupsMembershipsLookupCall

</details>


# 答案1
**得分**: 2

在Go语言中,如果一个名称以大写字母开头,那么它就是可导出的。这意味着你从导入的包中调用的任何方法都必须由这些包导出,并且必须以大写字母开头。这应该解决你的问题。请注意以下两者之间的区别:

- `.Groups.Memberships.Lookup`
- `.groups.memberships.lookup`

```go
package main

import (
	"context"
	"google.golang.org/api/cloudidentity/v1"
)

func main() {
	ctx := context.Background()
	svc, err := cloudidentity.NewService(ctx)
	if err != nil {
		panic(err)
	}
	res, err := svc.Groups.Memberships.Lookup("...").Context(ctx).Do()
	if err != nil {
		panic(err)
	}
	_ = res
}

希望对你有帮助!

英文:

In Go, a name is exported if it begins with a capital letter. This means that any methods you call from imported packages must be exported by those packages and therefore must start with capital letters. This should resolve your problem. Note the difference between

  • .Groups.Memberships.Lookup
  • .groups.memberships.lookup
package main

import (
	&quot;context&quot;
	&quot;google.golang.org/api/cloudidentity/v1&quot;
)

func main() {
	ctx := context.Background()
	svc, err := cloudidentity.NewService(ctx)
	if err != nil {
		panic(err)
	}
	res, err := svc.Groups.Memberships.Lookup(&quot;...&quot;).Context(ctx).Do()
	if err != nil {
		panic(err)
	}
	_ = res
}

huangapple
  • 本文由 发表于 2022年3月22日 05:13:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/71564197.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定