用户在我的android应用程序中授权。我正在将用户令牌和其他信息发送到我的服务器。在这个服务器上,我想为用户实现一些逻辑。如何在服务器上访问用户的日历信息?
我想正好有this flow。
我按照步骤 quickstart.php in this link获取用户的日历在服务器上。
,但我得到以下错误:
google oauth exception' with message 'could not json decode the token'
为此,我试过this solution。 但我采取相同的错误。所以作为第三个选项,我自己创建了json格式,如下面的this solution。
$access_token = '{
"access_token":'.$access_token.',
"token_type":"Bearer",
"expires_in":3600,
"id_token":'.$id_token.',
"refresh_token":" ",
"created":'. time() .'
}';
如你所见我不知道如何交换刷新令牌。我搜索了如何获得刷新令牌,并看到了this question。并执行this solution我的代码,但没有改变。
编辑4:我试图在Android应用程序获取访问令牌according to this answer并将其发送到应用程序服务器。我走的代码之前,我做的事:
GoogleSignInAccount acct = result.getSignInAccount();
code = acct.getServerAuthCode();//sending this code to AsyncTask to get access token
我的函数来获得访问令牌:
private void getAccessToken()throws GoogleAuthException, IOException{
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
List<String> scopes = new LinkedList<String>();
scopes.add("https://www.googleapis.com/auth/calendar");
scopes.add("https://www.googleapis.com/auth/calendar.readonly");
scopes.add("https://www.googleapis.com/auth/urlshortener");
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(transport, jsonFactory, client_id, client_secret, scopes).build();
try{
GoogleTokenResponse res = flow.newTokenRequest(code).execute();
accessToken = res.getAccessToken();
}catch(IOException e){
}
在PHP的服务器端,我改变user-example.php文件点点以下,因为我有现在访问令牌:
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setAccessType("offline");
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/urlshortener");
$service = new Google_Service_Urlshortener($client);
if (isset($_REQUEST['logout'])) {
unset($_SESSION['access_token']);
}
$client->setAccessToken('{"access_token":"'. $access_token .'","token_type":"Bearer","expires_in":3600,"created":'. time() .'}');
if ($client->getAccessToken() && isset($_GET['url'])) {
$url = new Google_Service_Urlshortener_Url();
$url->longUrl = $_GET['url'];
$short = $service->url->insert($url);
$_SESSION['access_token'] = $client->getAccessToken();
但现在我发现了以下错误:
Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/urlshortener/v1/url : (403) Insufficient Permission' in C:\wamp\www\google-php\src\Google\Http\REST.php on line 110
单引号中的变量不会被评估。打开变量的字符串。 '$ sp ='嗨,我是'。$ name'。我写字符串';' –
我仍然采取相同的json解码错误。 :\ – melomg
如果这两个标记不是整数,则将它们放入双引号中。 –