C2DM with PHP using OAuth2.0 (ClientLogin is depre

2020-05-23 11:44发布

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)

-- Original question --

Do we have example code for implementing PHP server-side code to push messages to Android devices using C2DM?

I have searched for many code samples which are using old authentication method ClientLogin which is deprecated. 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.)

3条回答
劳资没心,怎么记你
2楼-- · 2020-05-23 12:25

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.

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". Because it is trying to get this response from the body. Just simply comment the line in it, then run.

查看更多
不美不萌又怎样
3楼-- · 2020-05-23 12:27

That does not apply with C2DM since is a hosted accountif you see the https://developers.google.com/accounts/docs/AuthForInstalledApps it saids

ClientLogin can be used to authorize access to both Google regular and hosted accounts. A hosted account is a user account that is part of the Google Apps service.

Also, if you see the graphic is a clear interaction between User as a UI interface since a CAPTCHA is involved.

enter image description here

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. This is a pretty recent post from 8 days before the ClientLogin was deprecated. C2DM Client Login Key

查看更多
霸刀☆藐视天下
4楼-- · 2020-05-23 12:35

I couldn't figure out how to use C2DM with oAuth 2.0 yet but here's what I've tried. 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.

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)


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:

$ 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


oAuth 2.0: (Failed)

got an oAuth 2.0 auth credential from web browser by accessing url:

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/ )

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 :(


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. And ClientLogin deprecation policy will not apply to versions, features, and functionality labeled as "experimental." 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

查看更多
登录 后发表回答