【问题标题】:Is OAuth a must before using Google+API在使用 Google+API 之前必须使用 OAuth
【发布时间】:2013-01-09 12:31:21
【问题描述】:

我正在尝试将 get 和 list 方法与 google plus 评论一起使用。在官方网站上它说(所有 API 调用都需要 OAuth 2.0 令牌或 API 密钥。)并且我尝试在没有 OAuth 步骤的情况下发送 GET 请求,它可以返回 json 格式的数据。我的问题是在使用 google+ API 之前必须要求 OAuth?

【问题讨论】:

    标签: oauth google-plus


    【解决方案1】:

    这完全取决于您要获取的数据。

    https://developers.google.com/+/api/oauth 记录了使用 OAuth 的好处,但一般来说,如果您想获取私人个人资料数据,或者如果您希望使用 /me/ URL 快捷方式,您将需要使用 OAuth,并且可能,如果您希望,另外使用应用程序密钥。如果您只对公共数据感兴趣,则可以使用 App Key。

    【讨论】:

    • 我只想要来自 google + 的 cmets 是否意味着我可以跳过 OAuth 的部分。和 Google Plus 的令牌是什么似乎不相关?
    【解决方案2】:

    您是否可以做到这一点的简短答案是,您可以在没有 OAuth 的情况下从 Google+ 获取 cmets。

    至于你将如何做到这一点,我不确定你使用的是哪种语言,但下面的代码显示了如何在 JavaScript 中做到这一点。

    此处使用的 API 调用可以在 API 资源管理器中进行试验:

    demo of this code is here

    对于使用来自Google APIs console 的 Google+ API 的项目,您需要一个 API 密钥(简单密钥)。设置项目时,您只需从服务部分启用 Google+ API。

    首先,使用公共数据 API 抓取活动:

    // Gets the activities for a profile
    function getActivities(profileID){
      var activities = null;      
      var URL        = "https://www.googleapis.com/plus/v1/people/" + profileID +         "/activities/public?alt=json&key=" + key;
      var request = new XMLHttpRequest();
      request.open('GET', URL, false);
      request.send(); // because of "false" above, will block until the request is done 
                      // and status is available. Not recommended, however it works for simple cases.
    
      if (request.status === 200) {
        if (debug) console.log("retrieved activities \n\n");
        var activities = jQuery.parseJSON(request.responseText).items;
        console.log("Discovered " + activities.length + " activities");
      }else{
        handleRequestIssue(request);
      }
    
      return activities;
    }
    

    以下代码循环遍历活动

    for (var i=0; i < activities.length; i++) {
            console.log("trying to do something with an activity: " + i);
            var activity = activities[i];
    
            console.log(activity.id);
    }
    

    接下来,您可以使用活动 ID 来检索每个活动的 cmets:

    function getCommentsForActivity(activityID){
      var comments = "";      
      var URL        = "https://www.googleapis.com/plus/v1/activities/" + activityID + "/comments?alt=json&key=" + key;
      var request = new XMLHttpRequest();
      request.open('GET', URL, false);
      request.send(); // because of "false" above, will block until the request is done 
                  // and status is available. Not recommended, however it works for simple cases.
    
      if (request.status === 200) {
        if (debug) console.log(request.responseText);
        var comments  = jQuery.parseJSON(request.responseText).items;
    
        if (debug){
          for (comment in comments){
            console.log(comment);
          }
        } 
    
      }else{
        handleRequestIssue(request);
      }
    
      return comments;
    }
    
    
    function manualTrigger(){
      var activities = getActivities("109716647623830091721");
    }
    

    以下代码将所有内容组合在一起并检索特定帖子的活动和 cmets:

    $(document).ready(function () {
    
      var renderMe = "";
      var activities = getActivities("109716647623830091721");
    
      console.log("activities retrieved: " + activities.length);
    
      for (var i=0; i < activities.length; i++) {
        console.log("trying to do something with an activity: " + i);
        var activity = activities[i];
    
        renderMe += "<br/><div class=\"article\"><p>" + activity.title + "</p>";
        console.log(activity.id);
    
        // get comments
        var comments = getCommentsForActivity(activity.id);
        for (var j=0; j<comments.length; j++){
          renderMe += "<br/><div class=\"comment\">" + comments[j].object.content + "</div>";
        }
        renderMe += "</div>";
      }
      console.log("I'm done");
    
      document.getElementById("ac").innerHTML = renderMe;
    });
    

    【讨论】:

    • 这很好,因为 javascript 中的同源策略我不确定代码是否可以正常工作,但无论如何感谢您的帮助
    猜你喜欢
    • 2023-01-20
    • 2018-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多