duanjigua5753 2012-06-24 08:46
浏览 37
已采纳

使用客户端登录和PHP / cURL登录Google Spreadsheet API

I am using the ClientLogin method and cURL to login to the google API's. This works fine and I receive a Token for further usage. I can now query docs.google.com by using

        $curl = curl_init();

        $headers = array(
            "Authorization: GoogleLogin auth=" . $auth,
            "GData-Version: 3.0",
        );

        curl_setopt($curl, CURLOPT_URL, "https://docs.google.com/feeds/default/private/full");
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_POST, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        $response = curl_exec($curl);
        curl_close($curl);

This works fine and I get a list of all the documents available in my google docs account. But if I try the same query to spreadsheets.google.com with the URL obtained from the api documentation:

https://spreadsheets.google.com/feeds/spreadsheets/private/full

I get a 401 error saying that the token used is invalid. I am using the same token and query in both cases. Do I need a different token for the google spreadsheets api?

Edit: This is how I request the Token:

        $clientlogin_url = "https://www.google.com/accounts/ClientLogin";
        $clientlogin_post = array(
            "accountType" => "HOSTED_OR_GOOGLE",
            "Email" => "my email",
            "Passwd" => "my password",
            "service" => "writely",
            "source" => "my application name"
        );

        $curl = curl_init($clientlogin_url);

        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $clientlogin_post);
        curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

        $response = curl_exec($curl);

        preg_match("/Auth=([a-z0-9_-]+)/i", $response, $matches);
        $auth = $matches[1];
        curl_close($curl);
  • 写回答

1条回答 默认 最新

  • dpi74187 2012-06-24 09:48
    关注

    Short answer - Yes. You need to generate different tokens for different services. The service names you pass in to retrieve an auh token are different in each case. See here for more details - https://developers.google.com/gdata/faq

    For example from the docs the req for spreadsheets would be

    $clientlogin_post = array(
                "accountType" => "HOSTED_OR_GOOGLE",
                "Email" => "my email",
                "Passwd" => "my password",
                "service" => "wise",
                "source" => "my application name"
            );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题