如何在Google Play开发人员报告API中为令牌设置范围

发布于 2025-01-28 10:39:43 字数 2423 浏览 4 评论 0原文

我开始使用Google Play开发人员使用Golang( https://pkg.go.dev/google.golang.org/ ,并且面对与API范围有关的问题:

代码:

package main

import (
    "context"
    "encoding/json"
    "fmt"
    "golang.org/x/oauth2"
    "golang.org/x/oauth2/google"
    "golang.org/x/oauth2/jwt"
    "google.golang.org/api/option"
    "google.golang.org/api/playdeveloperreporting/v1beta1"
    "io/ioutil"
)

const (
    GoogleApplicationCredentials = "path_to_service_account_credentials"
    ProjectID                    = "apps/{project_id}"
)

func main() {
    tokenSource, err := getTokenSource(GoogleApplicationCredentials)
    if err !=nil {
        panic(err)
    }
    if err := getAnomalies(tokenSource, ProjectID); err != nil {
        panic(err)
    }
}

func getAnomalies(tokenSource oauth2.TokenSource, projectID string) error {
    ctx := context.Background()
    service, err := playdeveloperreporting.NewService(ctx, option.WithTokenSource(tokenSource))
    if err != nil {
        return err
    }
    anomaliesCall := service.Anomalies.List(projectID)
    result, err := anomaliesCall.Do()
    if err != nil {
        return err
    }
    fmt.Printf("\nStatus: %d", result.HTTPStatusCode)
    return nil
}

func getTokenSource(credentialFile string) (oauth2.TokenSource, error) {
    ctx := context.Background()
    b, err := ioutil.ReadFile(credentialFile)
    if err != nil {
        return nil, err
    }
    var c = struct {
        Email      string `json:"client_email"`
        PrivateKey string `json:"private_key"`
    }{}
    if err := json.Unmarshal(b, &c); err != nil {
        return nil, err
    }
    fmt.Printf("\nClient email: %s\n", c.Email)
    config := &jwt.Config{
        Email:      c.Email,
        PrivateKey: []byte(c.PrivateKey),
        Scopes:     []string{
            "?????????",
        },
        TokenURL: google.JWTTokenURL,
    }
    return config.TokenSource(ctx), nil
}

我的问题是我需要使用什么范围?我没有在 - https://developers.google.com/indesity/protocors.com/protolotsss/protocors /oauth2/scopes

谢谢

I started to use Google Play Developer Reporting API with using Golang(https://pkg.go.dev/google.golang.org/[email protected]/playdeveloperreporting/v1beta1), and faced with issue related to API scopes:

Code:

package main

import (
    "context"
    "encoding/json"
    "fmt"
    "golang.org/x/oauth2"
    "golang.org/x/oauth2/google"
    "golang.org/x/oauth2/jwt"
    "google.golang.org/api/option"
    "google.golang.org/api/playdeveloperreporting/v1beta1"
    "io/ioutil"
)

const (
    GoogleApplicationCredentials = "path_to_service_account_credentials"
    ProjectID                    = "apps/{project_id}"
)

func main() {
    tokenSource, err := getTokenSource(GoogleApplicationCredentials)
    if err !=nil {
        panic(err)
    }
    if err := getAnomalies(tokenSource, ProjectID); err != nil {
        panic(err)
    }
}

func getAnomalies(tokenSource oauth2.TokenSource, projectID string) error {
    ctx := context.Background()
    service, err := playdeveloperreporting.NewService(ctx, option.WithTokenSource(tokenSource))
    if err != nil {
        return err
    }
    anomaliesCall := service.Anomalies.List(projectID)
    result, err := anomaliesCall.Do()
    if err != nil {
        return err
    }
    fmt.Printf("\nStatus: %d", result.HTTPStatusCode)
    return nil
}

func getTokenSource(credentialFile string) (oauth2.TokenSource, error) {
    ctx := context.Background()
    b, err := ioutil.ReadFile(credentialFile)
    if err != nil {
        return nil, err
    }
    var c = struct {
        Email      string `json:"client_email"`
        PrivateKey string `json:"private_key"`
    }{}
    if err := json.Unmarshal(b, &c); err != nil {
        return nil, err
    }
    fmt.Printf("\nClient email: %s\n", c.Email)
    config := &jwt.Config{
        Email:      c.Email,
        PrivateKey: []byte(c.PrivateKey),
        Scopes:     []string{
            "?????????",
        },
        TokenURL: google.JWTTokenURL,
    }
    return config.TokenSource(ctx), nil
}

My question is what scope I need to use? I didn't find in - https://developers.google.com/identity/protocols/oauth2/scopes

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请恋爱 2025-02-04 10:39:43

当您在这种情况下访问API时数据是私人用户数据。为了访问私人用户数据,您的应用程序需要该数据所有者或具有访问权限的人的权限。

为了获得该访问,我们使用oauth2,并非所有方法都相等地相等,具体取决于 Google Play开发人员报告API 您正在尝试使用将决定用户将需要授予您的访问范围。

判断哪个范围的最简单方法是检查文档,

让我们查看 anmalies.list 示例的方法。如果我们向下滚动到底部,它将告诉您用户需要授权的哪个范围。

进行了一些检查后,我认为此API只有一个范围,它是https://www.googleapis.com/auth/playdeveloperrereporting。因此,如果您要求该范围,则应可以访问完整的API。

When you access an api in this case Google Play Developer Reporting API most of the data is private user data. In order to access private user data your application needs the permissions of the owner of that data or someone with access.

To get that access we use Oauth2, not all methods are created equal depending upon which method within the Google Play Developer Reporting API you are trying to use will dictate which scope of access the user will need to grant you.

The easest way to tell which scope is to check the documentation

Lets look at the anomalies.list method for an example. If we scroll down to the bottom it tells you exactly which scope your user needs to authorize.

enter image description here

After a bit of checking i think there is only one scope for this api it is https://www.googleapis.com/auth/playdeveloperreporting. So if you request that scope then you should have access to the full api.

剩余の解释 2025-02-04 10:39:43

我有相同的问题

范围
https://www.googleapis.com/auth/auth/playdeveveloperreportering

您要么可以收到此错误您的输入OAuth2范围名称无效,或者是指在此旧式API领域之外的较新范围。

该API是在范围名称格式尚未标准化的时候构建的。情况不再如此,所有有效的范围名称(既旧)名称均在”上分类。 https://developers.google.com/indentity/protocols/oauth2/scopes 。使用该网页(手动)查找与您要致电并使用它来制作OAuth2请求的API关联的范围名称。

I had the same problem

scope
https://www.googleapis.com/auth/playdeveloperreporting

You are receiving this error either because your input OAuth2 scope name is invalid or it refers to a newer scope that is outside the domain of this legacy API.

This API was built at a time when the scope name format was not yet standardized. This is no longer the case and all valid scope names (both old and new) are catalogued at https://developers.google.com/identity/protocols/oauth2/scopes. Use that webpage to lookup (manually) the scope name associated with the API you are trying to call and use it to craft your OAuth2 request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文