在config.json文件中输入自定义类型值。

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

entering custom type values in a config.json file

问题

我正在尝试使用用Go编写的这个论坛软件,它有一个配置文件,需要在main.go中看到的值。我尝试在我的本地机器上使用空字符串""作为空字符串进行测试,但是我收到了一个错误:

json: 无法将字符串解组为类型为oauth.Credentials的Go值

所以,即使我将其部署到服务器上,我认为我也必须将凭据输入为字符串,这样也会触发错误。

假设我的API密钥和密钥如下:

key= X482xYAFG1I2RKBYR
secret = 4929390593pqI4wNMljlj4N71oyOdlWCzyNKhv4BAd4QXLvW2LF

考虑到下面的main.go的要求,你会如何在config.json文件中输入凭据?

{
    "TwitterOAuthCredentials":,
    "CookieAuthKeyHexStr":"2ded82bb63514dbd6a0af42a25df235c",
    "CookieEncrKeyHexStr":"4add93d1d6bb3489c9b3ab5448704068",
    "AnalyticsCode":"",
    "AwsAccess":"",
    "AwsSecret":"",
    "S3BackupBucket":"",
    "S3BackupDir": ""
}

来自main.go的配置要求:

config = struct {
    TwitterOAuthCredentials *oauth.Credentials
    CookieAuthKeyHexStr     *string
    CookieEncrKeyHexStr     *string
    AnalyticsCode           *string
    AwsAccess               *string
    AwsSecret               *string
    S3BackupBucket          *string
    S3BackupDir             *string
}{
    &oauthClient.Credentials,
    nil, nil,
    nil,
    nil, nil,
    nil, nil,
}
英文:

I'm trying to use this forum software written in Go that has a config file that requires the values you see in main.go below. I tried to use an empty string "" for the oauth credentials to play around on my local machine but I got an error

json. json: cannot unmarshal string into Go value of type oauth.Credentials

So, even if I were to deploy it on a server, I thought I would have had to enter the credentials as a string, which would trigger the error there as well.

Assuming my api key and secret were like this

key= X482xYAFG1I2RKBYR
secret = 4929390593pqI4wNMljlj4N71oyOdlWCzyNKhv4BAd4QXLvW2LF

Taking into consideration the requirements from main.go below, how would you enter the credentials in a config.json file like this

{
	"TwitterOAuthCredentials":,
	"CookieAuthKeyHexStr":"2ded82bb63514dbd6a0af42a25df235c",
	"CookieEncrKeyHexStr":"4add93d1d6bb3489c9b3ab5448704068",
	"AnalyticsCode":"",
	"AwsAccess":"",
	"AwsSecret":"",
	"S3BackupBucket":"",
	"S3BackupDir": ""
}

config requirements from main.go

config = struct {
	TwitterOAuthCredentials *oauth.Credentials
	CookieAuthKeyHexStr     *string
	CookieEncrKeyHexStr     *string
	AnalyticsCode           *string
	AwsAccess               *string
	AwsSecret               *string
	S3BackupBucket          *string
	S3BackupDir             *string
}{
	&oauthClient.Credentials,
	nil, nil,
	nil,
	nil, nil,
	nil, nil,
}

答案1

得分: 1

如果你不想设置它,可以完全省略,它将被设置为默认值。

否则,请输入一个oauth.Credentials的JSON表示,它包含一个Token和一个Secret字符串:

type Credentials struct {
    Token  string // 也称为消费者密钥或访问令牌。
    Secret string // 也称为消费者密钥或访问令牌密钥。
}

在你的配置中,凭据应该如下所示:

"TwitterOAuthCredentials": {
    "Token": "oauthtoken",
    "Secret": "oauthsecret"
}
英文:

If you don't want to set it, leave it out entirely, and it will get set to the default value.

Otherwise, enter the json representation of an oauth.Credentials, which is just a Token and a Secret string:

type Credentials struct {
	Token  string // Also known as consumer key or access token.
	Secret string // Also known as consumer secret or access token secret.
}

In your config, the credentials would look like:

"TwitterOAuthCredentials": {
    "Token": "oauthtoken",
    "Secret": "oauthsecret"
},

huangapple
  • 本文由 发表于 2014年7月25日 21:37:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/24957074.html
匿名

发表评论

匿名网友

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

确定