如何在Google Play开发者报告API中设置令牌的范围

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

How to set scope for token in Google Play Developer Reporting API

问题

我开始使用Google Play开发者报告API,并使用Golang(https://pkg.go.dev/google.golang.org/api@v0.79.0/playdeveloperreporting/v1beta1)。我遇到了与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/identity/protocols/oauth2/scopes中没有找到。

谢谢。

英文:

I started to use Google Play Developer Reporting API with using Golang(https://pkg.go.dev/google.golang.org/api@v0.79.0/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

答案1

得分: 1

当您访问此案例中的 API(Google Play 开发者报告 API)时,大部分数据都是私人用户数据。为了访问私人用户数据,您的应用程序需要该数据的所有者或具有访问权限的人的权限。

为了获得访问权限,我们使用 OAuth2,根据您尝试使用的 Google Play 开发者报告 API 中的方法的不同,将决定用户需要授予您哪个访问范围。

判断所需访问范围的最简单方法是查看文档。

让我们以 anomalies.list 方法为例。如果我们向下滚动到底部,它会告诉您用户需要授权的确切访问范围。

如何在Google Play开发者报告API中设置令牌的范围

经过一番检查,我认为此 API 只有一个访问范围,即 https://www.googleapis.com/auth/playdeveloperreporting。因此,如果您请求该访问范围,您将可以访问完整的 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.

如何在Google Play开发者报告API中设置令牌的范围

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.

答案2

得分: 0

我遇到了同样的问题

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

您收到此错误是因为您输入的OAuth2范围名称无效,或者它引用了一个超出此旧版API范围的新版范围。

此API是在范围名称格式尚未标准化的时候构建的。现在情况已经不同,所有有效的范围名称(包括旧版和新版)都在https://developers.google.com/identity/protocols/oauth2/scopes上进行了归档。请使用该网页手动查找与您尝试调用的API相关联的范围名称,并将其用于构建您的OAuth2请求。

英文:

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.

huangapple
  • 本文由 发表于 2022年5月11日 18:26:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/72199410.html
匿名

发表评论

匿名网友

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

确定