java 什么是授权实体?在自己的应用程序中找不到 gcm_defaultSenderId
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30653130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is authorizedEntity? Can't find gcm_defaultSenderId in own app
提问by sitting-duck
I am trying to get my app running with Google Cloud Messaging. I am following the Google Cloud Messaging Quickstart App which can be found here on github.
我正在尝试使用 Google Cloud Messaging 运行我的应用程序。我正在关注可以在github上找到的 Google Cloud Messaging Quickstart App 。
In their quickstart app at some point we ask the Google Cloud Messaging service for a registration token so that this instance of our app can talk to the cloud.
在他们的快速入门应用程序中,我们有时会要求 Google Cloud Messaging 服务提供注册令牌,以便我们的应用程序实例可以与云通信。
I find this line of code:
我发现这行代码:
RegistrationIntentService.java::onHandleIntent(Intent intent):
InstanceID instanceID = InstanceID.getInstance(this);
String gcmRegistrationToken = instanceID.getToken(getString(R.string.gcm_defaultSenderId),
GoogleCloudMessaging.INSTANCE_ID_SCOPE, null);
The part that is confusing me is this value: R.string.gcm_defaultSenderId
It is defined in their quickstart app, but it is automatically generated.
令我困惑的部分是这个值:R.string.gcm_defaultSenderId
它在他们的快速入门应用程序中定义,但它是自动生成的。
How am I supposed to get my app to generate that value?
我应该如何让我的应用产生那个价值?
I look up the docs for InstanceID.getToken which is heregetToken(java.lang.String, java.lang.String)
我在此处查找 InstanceID.getToken 的文档getToken(java.lang.String, java.lang.String)
InstanceID.getoken
returns a token that authorizes an Entity (example: cloud service) to perform an action on behalf of the application identified by Instance ID.
This is similar to an OAuth2
token except, it applies to the application instance instead of a user.
InstanceID.getoken
返回一个令牌,该令牌授权实体(例如:云服务)代表由实例 ID 标识的应用程序执行操作。这类似于OAuth2
令牌,不同之处在于它适用于应用程序实例而不是用户。
The function header looks like:
函数头看起来像:
public String getToken (String authorizedEntity, String scope)
I see that the first arg that getToken
wants is String authorizedEntity
.
so, what is this authorizedEntity String supposed to be?
It clearly identifies the instance of the app making the request, but how am I supposed to generate it? In the quickstart app, I can't find it defined in res/value/strings.xml
, I can only find it defined in R.java and app/build/generated/res/google-services/debug/values/values.xml
我看到getToken
想要的第一个 arg是String authorizedEntity
. 那么,这个authorizedEntity String 应该是什么?
它清楚地标识了发出请求的应用程序的实例,但我应该如何生成它?在快速入门应用程序中,我找不到它的定义res/value/strings.xml
,我只能找到它在 R.java 和app/build/generated/res/google-services/debug/values/values.xml
It looks like:
看起来像:
<resources>
<string name="gcm_defaultSenderId">175643285</string>
</resources>
There is just that one string in that file, and that file is buried way deep in the project structure.
I can't find anywhere in the code where this gcm_defaultSenderId
is being generated programmatically.
该文件中只有一个字符串,该文件深埋在项目结构中。我在以gcm_defaultSenderId
编程方式生成的代码中找不到任何地方。
I'm confused because, how was I supposed to know that string was there? I never defined that string, and googling for "cannot resolve gcm_DefaultSenderId
" gives no results. I'm trying to implement Google Cloud Messaging in my own app, so of course my own app is not going to automatically know to generate that string. How am I supposed to make that id number?
我很困惑,因为我怎么知道那里有那个字符串?我从来没有定义过那个字符串,谷歌搜索“无法解析gcm_DefaultSenderId
”没有结果。我正在尝试在我自己的应用程序中实现 Google Cloud Messaging,所以当然我自己的应用程序不会自动知道生成该字符串。我应该如何制作那个身号码?
This is why I think it's important that I understand what this authorizedEntity string that InstanceID.getToken wants, so that I can properly generate one to give to getToken. Perhaps my idea is completely wrong, perhaps I am not supposed to generate gcm_defaultSenderId, but I know that I am not supposed to alter R.java, and the values.xml
file is also under a "generated" folder.
这就是为什么我认为了解 InstanceID.getToken 想要的这个授权实体字符串很重要的原因,以便我可以正确生成一个提供给 getToken。也许我的想法完全错误,也许我不应该生成gcm_defaultSenderId,但是我知道我不应该更改R.java,并且该values.xml
文件也在“生成”文件夹下。
Help please? If I find the answer in my searches, I will happily post the answer. Any help much appreciated, note: my project was exported to Gradlefrom Eclipse, so it will still have the Eclipse project/folder structure, that shouldn't cause any problems, but the values.xml
file is in a different place.
请帮忙?如果我在搜索中找到答案,我会很乐意发布答案。非常感谢任何帮助,请注意:我的项目是从Eclipse导出到Gradle 的,因此它仍然具有 Eclipse 项目/文件夹结构,这不会导致任何问题,但文件位于不同的位置。values.xml
回答by peter.bartos
The R.string.gcm_defaultSenderId
value is generated by the Gradle google-services plugin which uses a google-services.json
file with defined constants.
该R.string.gcm_defaultSenderId
值由 Gradle google-services 插件生成,该插件使用google-services.json
具有定义常量的文件。
The plugin is applied in Gradle:
该插件在 Gradle 中应用:
apply plugin: 'com.google.gms.google-services'
For more info check
Implementing GCM Client on Androidand see how to get the google-services.json
file and set-up Gradle & app in developer console.
有关更多信息,请查看
在 Android 上实现 GCM 客户端,并了解如何google-services.json
在开发者控制台中获取文件和设置 Gradle 和应用程序。
回答by Ramesh
It is the project id that we need to fill in place of that string. Please refer to the following link.
我们需要填写项目 ID 来代替该字符串。请参考以下链接。
https://developers.google.com/instance-id/guides/android-implementation
https://developers.google.com/instance-id/guides/android-implementation
Generating tokens requires a Project ID generated by the Google Developers Console.
生成令牌需要 Google Developers Console 生成的项目 ID。
String authorizedEntity = PROJECT_ID; // Project id from Google Developers Console
String scope = “GCM”; // e.g. communicating using GCM, but you can use any
// URL-safe characters up to a maximum of 1000, or
// you can also leave it blank.
String token = InstanceID.getInstance().getToken(authorizedEntity,scope);