简体   繁体   English

使用OAuth2.0使用PHP的C2DM(不推荐使用ClientLogin!)

[英]C2DM with PHP using OAuth2.0 (ClientLogin is deprecated!)

Note: Before you spend your time reading on, please know that C2DM is itself deprecated now and replaced by GCM (http://developer.android.com/guide/google/gcm/c2dm.html) 注意:在您花时间阅读之前,请注意C2DM现在已被弃用,并由GCM取代(http://developer.android.com/guide/google/gcm/c2dm.html)

-- Original question -- - 原始问题 -

Do we have example code for implementing PHP server-side code to push messages to Android devices using C2DM? 我们是否有实现PHP服务器端代码的示例代码,以便使用C2DM将消息推送到Android设备?

I have searched for many code samples which are using old authentication method ClientLogin which is deprecated. 我搜索了许多使用旧的身份验证方法ClientLogin的代码示例,该方法已被弃用。 Reference: https://developers.google.com/accounts/docs/AuthForInstalledApps (Important: ClientLogin has been officially deprecated as of April 20, 2012. It will continue to work as per our deprecation policy, but we encourage you to migrate to OAuth 2.0 as soon as possible.) 参考: https//developers.google.com/accounts/docs/AuthForInstalledApps (重要提示:截至2012年4月20日,ClientLogin已正式弃用。它将继续按照我们的弃用政策运行,但我们建议您迁移到OAuth 2.0尽快。)

That does not apply with C2DM since is a hosted accountif you see the https://developers.google.com/accounts/docs/AuthForInstalledApps it saids 这不适用于C2DM,因为它是一个托管的accountif,你看到https://developers.google.com/accounts/docs/AuthForInstalledApps它说

ClientLogin can be used to authorize access to both Google regular and hosted accounts. ClientLogin可用于授权访问Google常规帐户和托管帐户。 A hosted account is a user account that is part of the Google Apps service. 托管帐户是属于Google Apps服务的用户帐户。

Also, if you see the graphic is a clear interaction between User as a UI interface since a CAPTCHA is involved. 此外,如果您看到图形是用户作为UI界面之间的明确交互,因为涉及CAPTCHA。

在此输入图像描述

Edit 编辑

BTW you can see from this post C2DM mechanism still will be using ClientLogin nevertheless the key before October 2011 were going to expired, so you need to recreate them. 顺便说一下你可以从这篇帖子看到C2DM机制仍然会使用ClientLogin但是2011年10月之前的密钥将要过期,所以你需要重新创建它们。 This is a pretty recent post from 8 days before the ClientLogin was deprecated. 这是在不推荐使用ClientLogin之前8天的最新帖子。 C2DM Client Login Key C2DM客户端登录密钥

I couldn't figure out how to use C2DM with oAuth 2.0 yet but here's what I've tried. 我无法弄清楚如何将C2DM与oAuth 2.0一起使用,但这是我尝试过的。 Hope this can help someone to solve similar problem 希望这可以帮助别人解决类似的问题

I found a resource that would be useful at http://aleksmaus.blogspot.com/2012/01/oauth2-with-google-c2dm-push.html But when I tried to send message with C2DM through OAuth 2.0 it didn't work while did a good job with ClientLogin. 我找到了一个在http://aleksmaus.blogspot.com/2012/01/oauth2-with-google-c2dm-push.html上有用的资源但是当我尝试通过OAuth 2.0发送带有C2DM的消息时它没有使用ClientLogin做得很好。

This is how I did with console and web browser (I know, you've asked PHP implementation. But I hope this can also be helpful for you) 这就是我使用控制台和Web浏览器的方式(我知道,您已经问过PHP实现。但我希望这对您也有帮助)


Client Login: (Succeeded) 客户登录:(成功)

got auth token from: 得到身份验证令牌:

$ curl -k -d "accountType=HOSTED_OR_GOOGLE&service=ac2dm&source=test-1.0&Email=[email account with @gmail.com without brace]&Passwd=[Google account password without brace]" https://www.google.com/accounts/ClientLogin 

And sent C2DM message like this: 并发送C2DM消息如下:

$ curl -k --header "Authorization: GoogleLogin auth=[my ClientLogin auth key without brace]" -d "registration_id=[can be acquired from Android application]" --trace c2dm_trace.txt -d collapse_key=0 https://android.apis.google.com/c2dm/send

Then my application successfully received C2DM message 然后我的应用程序成功收到C2DM消息


oAuth 2.0: (Failed) oAuth 2.0 :(失败)

got an oAuth 2.0 auth credential from web browser by accessing url: 通过访问url从Web浏览器获得oAuth 2.0 auth凭证:

https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=[can be acquired from API Access menu in your API Console]&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https%3A%2F%2Fandroid.apis.google.com%2Fc2dm&access_type=offline

(Google API Console: https://code.google.com/apis/console/ ) (Google API控制台: https//code.google.com/apis/console/

And sent like this: 并发送这样的:

$ curl -k -H "Authorization: Bearer [my auth key from oAuth]" --trace curl_trace.txt -d "registration_id=[an be acquired from Android application]" -d "data.message=something to talk" -d collapse_key=0 https://android.apis.google.com/c2dm/send

Then got 401 error response :( 然后得到401错误响应 :(


Is there anybody who can point out what I've done something wrong? 有没有人可以指出我做错了什么?

Edit 编辑

I've found the sentence "AC2DM is currently an API in Labs" in the mail that you might received from Google when register C2DM. 我在注册C2DM时可能会从Google收到的邮件中找到“AC2DM目前是实验室中的API”这句话。 And ClientLogin deprecation policy will not apply to versions, features, and functionality labeled as "experimental." 并且ClientLogin弃用政策不适用于标记为“实验”的版本,功能和功能。 I'm not sure but I think this is why our codes didn't work. 我不确定,但我认为这就是我们的代码不起作用的原因。

Of course, I hope ClientLogin also be deprecated and replaced with OAuth 2.0 soon 当然,我希望不久也会弃用ClientLogin并替换为OAuth 2.0

Here 这里

you can take a look, it worked for me. 你可以看看,它对我有用。

You need to get your device registrationId,and enter your c2dm account email address and password to post.php. 您需要获取设备registrationId,并输入您的c2dm帐户电子邮件地址和密码到post.php。

And the other thing that you need to know is about c2dm.php, there is an error occurs when it tries to check out server response as "200 OK". 另外你需要知道的是关于c2dm.php,当它试图将服务器响应检出为“200 OK”时会发生错误。 Because it is trying to get this response from the body. 因为它试图从身体得到这种反应。 Just simply comment the line in it, then run. 只需在其中注释该行,然后运行即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM