我怎样才能达到服务器用户的日历信息? [英] How can I reach users' calendar information on server?

查看:219
本文介绍了我怎样才能达到服务器用户的日历信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

授权的用户在我的Andr​​oid应用程序。而且我发送用户的令牌和其他信息到我的服务器。在这台服务器我要实现对用户的一些逻辑。

我想有完全<一个href=\"http://stackoverflow.com/questions/26351730/how-to-insert-events-into-google-calendar-from-server-with-client-token\">this流程。

我跟着这个链接步骤 quickstart.php让服务器用户的日历。

但是我收到以下错误:


  

谷歌OAuth的异常'有消息'不能JSON德code令牌


为此,我试过这个解决方案
而是我拿同样的错误。因此,作为第三个选项我创建了JSON格式自己像这个解决方案如下。

  $的access_token ='{
    的access_token:$的access_token。
    token_type:旗手,
    expires_in:3600,
    id_token:$ id_token。
    refresh_token:,
    创造:。时间() 。'
};

你看,我不知道如何来交换刷新令牌。我搜索如何获得刷新令牌,看到这个问题。实施该解决方案以我的code,但什么都没有改变。

修改4 :我试图根据在android应用这个答案以获得访问令牌并将其发送到应用服务器。我走的是code像我一样以前:

  GoogleSignInAccount ACCT = result.getSignInAccount();
code = acct.getServerAuth code(); //发送此code到的AsyncTask获得访问令牌

我的函数来获得访问令牌:

 私人无效getAccessToken()抛出GoogleAuthException,IOException异常{
    新的AsyncTask&LT;太虚,太虚,字符串&GT;(){
        @覆盖
        保护字符串doInBackground(虚空...... PARAMS){
            清单&LT;串GT;范围=新的LinkedList&LT;串GT;();
                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);                GoogleAuthorization codeFLOW流量=新GoogleAuthorization codeFlow.Builder(运输,jsonFactory,CLIENT_ID,client_secret,范围).build();                尝试{
                    GoogleTokenResponse解析度= flow.newTokenRequest(code).execute();
                    的accessToken = res.getAccessToken();
                }赶上(IOException异常五){
                }

在PHP的服务器端,我改变的用户使用example.php 文件小,如下一点,因为我有访问令牌现在:

  $客户=新Google_Client();
$客户 - &GT; SETCLIENTID($ CLIENT_ID);
$客户 - &GT; setClientSecret($ client_secret);
$客户 - &GT; setAccessType(离线);
$客户 - &GT; setRedirectUri($ REDIRECT_URI);
$客户 - &GT; addScope(https://www.googleapis.com/auth/urlshortener);
$ =服务新Google_Service_Urlshortener($客户端);
如果(使用isset($ _ REQUEST ['注销'])){
    未设置($ _ SESSION ['ACCESS_TOKEN']);
}
$客户 - &GT; setAccessToken({的access_token:'。$的access_token',token_type:旗手,expires_in:3600,创造:时间()。'}') ;
如果($客户 - &GT; getAccessToken()及和放大器;使用isset($ _ GET ['URL'])){
$ URL =新Google_Service_Urlshortener_Url();
$ URL-&GT; longUrl = $ _GET ['URL'];
$短= $服务 - &GT; URL-&GT;插入($网址);
$ _SESSION ['ACCESS_TOKEN'] = $客户 - &GT; getAccessToken();

但现在我收到以下错误:


  

致命错误:未捕获的异常'Google_Service_Exception有消息错误调用POST 的https:// WWW。 googleapis.com/urlshortener/v1/url :(403)权限不足在C:\\ WAMP \\ WWW \\谷歌的PHP的\\ src \\谷歌\\ HTTP \\ REST.php线110



解决方案

我越来越没有足够的权限错误后我开始使用GoogleAuthorization codeFLOW正如我在OP提到获得访问令牌。然后我试图日历范围添加到 GoogleApiClient.Builder(本),但我得到的错误一样,如果我加入验证我不能添加范围。 GOOGLE_SIGN_IN_API ,因为我已经加入 Auth.GOOGLE_SIGN_IN_API 来GoogleApiClient.Builder。所以这次我尝试添加的范围GoogleSignInOptions.Builder,现在正在工作。我能够同时获得刷新令牌和访问令牌。低于code解决我的问题:

  GoogleSignInOptions GSO = state.getGso();
如果(GSO == NULL){
    GSO =新GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestScopes(新范围(https://www.googleapis.com/auth/calendar))
        .requestIdToken(的getString(R.string.server_client_id))
        .requestEmail()
        .requestServerAuth code(的getString(R.string.server_client_id),FALSE)
        .requestProfile()
        。建立();
}

Users authorize in my android application. And I am sending users' token and other information to my server. At this server I want to implement some logic for users.

I want to have exactly this flow.

I followed the steps quickstart.php in this link to get users' calendars on server.

But I get following error :

google oauth exception' with message 'could not json decode the token'

For this reason I tried this solution. But i take same error. So as 3rd option I created the json format myself like in this solution as below.

$access_token = '{
    "access_token":'.$access_token.',
    "token_type":"Bearer",
    "expires_in":3600, 
    "id_token":'.$id_token.', 
    "refresh_token":" ",
    "created":'. time() .'
}';

as you see I do not know how to exchange refresh token . I searched how to get refresh token and saw this question. And implemented this solution to my code but nothing changed.

Edit 4 : I tried to get access token according to this answer at android application and send it to app server. I'm taking the code as before I did :

GoogleSignInAccount acct = result.getSignInAccount();
code = acct.getServerAuthCode();//sending this code to AsyncTask to get access token

my function 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){
                }

at the php server side I changed user-example.php file little bit as below because I have the access token now:

$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();

But now I'm getting below error:

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

解决方案

I was getting Insufficient Permission error after I started to use GoogleAuthorizationCodeFlow to get access token as I mentioned in my OP. And then I tried to add Calendar scope to GoogleApiClient.Builder(this) but I get error like I can't add scope if I add Auth.GOOGLE_SIGN_IN_API because I had added Auth.GOOGLE_SIGN_IN_API to GoogleApiClient.Builder. So this time I tried to add the scope to GoogleSignInOptions.Builder and it is working now. I'm able to get both refresh token and access token. Below code solved my problem:

GoogleSignInOptions gso = state.getGso();
if(gso == null){
    gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestScopes(new Scope("https://www.googleapis.com/auth/calendar"))
        .requestIdToken(getString(R.string.server_client_id))
        .requestEmail()
        .requestServerAuthCode(getString(R.string.server_client_id), false)
        .requestProfile()
        .build();
}

这篇关于我怎样才能达到服务器用户的日历信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆