提问者:小点点

如何以编程方式访问谷歌服务中的谷歌API密钥。json


该应用程序使用谷歌云消息,并包含一个谷歌服务。json文件,它本身包含Google API身份验证信息,包括API密钥。GCM按预期运行。

该应用程序包含一个使用HTTP发送GCM消息的测试工具,它需要API密钥。如何访问存储在google services中的密钥。json?我不想使用GoogleCloudMessaging发送消息(我使用的是OkHttp,它做得很好)。

希望没有必要在应用程序中复制它。


共2个答案

匿名用户

抱歉,比赛有点晚了...

google-services.json文件被解析,其值被添加到您可以访问的xml中:https://developers.google.com/android/guides/google-services-plugin

在您的例子中,您可以通过以下方式获取api密钥:activity.getResources(). getString(R.string.google_api_key);

匿名用户

您只需从GoogleCloud平台获取API密钥,然后转到凭据,就可以从那里获取API密钥。https://console.developers.google.com/

这就是你要找的吗?

但是如果您需要访问它(除非您更改json文件本身,否则它永远不会更改),那么您可以使用解析器解析json文件。但这增加了比需要更多的工作。我只是从网上复制粘贴它。

好的,我明白你要做什么了,试着使用这个,并像这样使用NSDefualts商店令牌:

 if (registrationToken != nil) {
        self.registrationToken = registrationToken
        print("Registration Token: \(registrationToken)")
        //store the registation token for use in the postData function in the login page
        self.loginInformation.setObject(self.registrationToken, forKey: "GCMToken")
        let userInfo = ["registrationToken": registrationToken]

并在项目中的任何地方检索它,就像这样:

myNoteGCMID = loginInformation.objectForKey("GCMToken") as! String

如果它改变了,你不必改变它,只要在一个地方。无论你称之为哪里,变化都会发生。