我还不知道如何使用oAuth 2.0来使用C2DM,但是我尝试过以下方法。希望这能帮助解决类似的问题。
我找到了一个有用的资源,网址为http://aleksmaus.blogspot.com/2012/01/oauth2-with-google-c2dm-push.html。但是当我尝试通过OAuth 2.0发送C2DM消息时,它没有成功,而使用ClientLogin则可行。
这是我在控制台和Web浏览器中的做法
(我知道,您要求PHP实现。但我希望这对您也有所帮助)
客户端登录:(成功)
从以下位置获取授权令牌:
$ 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
并发送类似以下的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
我的应用程序成功接收到了C2DM消息。
oAuth 2.0:(失败)
通过访问以下URL从Web浏览器获取了oAuth 2.0授权凭据:
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 控制台:https://code.google.com/apis/console/ )
并像这样发送:
$ 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
然后收到了401错误响应 :(
有人能指出我做错了什么吗?
编辑
我在注册C2DM时的邮件中发现了句子"AC2DM目前是实验性API"。而且客户端登录弃用政策不适用于被标记为“实验”的版本、功能和特性。我不确定,但我认为这就是我们的代码无法工作的原因。
当然,我希望ClientLogin也很快被OAuth 2.0取代并弃用。