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

后端 未结 3 1697
盖世英雄少女心
盖世英雄少女心 2021-02-04 12:01

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)

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-04 12:30

    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

提交回复
热议问题