23

国民の祝日を含む公開カレンダー (Google カレンダーから) にアクセスしようとしています:

calendarId: 'pt_br.brazilian#holiday@group.v.calendar.google.com'

カレンダーは公開されているため、API キーのみを使用してアクセスできると思いました。

function OnLoadCallback() {
    var config = {
        client_id: '32j4lk32j5kj342l5h.googleuser.com', //fake client id
        scope: 'https://www.googleapis.com/auth/calendar.readonly'
    };
    gapi.client.setApiKey('fId345AM20HXXXXXXXXXXXXXXXXgT3f9kyp2REfkaw2'); //fake api key
    gapi.client.load('calendar', 'v3', function() {
        var today = new Date(),
            request;

        request = gapi.client.calendar.calendarList.get({
            calendarId: 'pt_br.brazilian#holiday@group.v.calendar.google.com',
            timeMin: (new Date(today.getFullYear(), today.getMonth(), today.getDay(), 0, 0, 0, 0)).toISOString(),
            timeMax: (new Date(today.getFullYear(), today.getMonth(), today.getDay(), 23, 59, 59, 999)).toISOString(),
            fields: 'items(creator(displayName,email),end,endTimeUnspecified,start,summary)'
        });

        request.execute(function(response) {
            window.alert('length of items: ' + response.items.length);
        });

    });
}

ただし、次の応答が返されます。これは 401 (権限のない) エラーです。

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Login Required",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Login Required"
 }
}

私がやろうとしていることが達成可能かどうかを誰かが明確にすることはできますか?
そして最後に、可能であれば、現在のコードを参照して何を変更する必要がありますか?

4

2 に答える 2

37

jQueryを使用して同じことを行うことができました。

var mykey = 'your_api_key'; // typically like Gtg-rtZdsreUr_fLfhgPfgff
var calendarid = 'you_calendar_id'; // will look somewhat like 3ruy234vodf6hf4sdf5sd84f@group.calendar.google.com

$.ajax({
    type: 'GET',
    url: encodeURI('https://www.googleapis.com/calendar/v3/calendars/' + calendarid+ '/events?key=' + mykey),
    dataType: 'json',
    success: function (response) {
        //do whatever you want with each
    },
    error: function (response) {
        //tell that an error has occurred
    }
});

ただし、次のことを確認する必要があります。-


1) https://code.google.com/apis/consoleでプロジェクトを登録します
。2)シンプルなAPIアクセスキーを生成します
。3)サービスでCalendarAPIがアクティブになっていることを確認します。

https://developers.google.com/google-apps/calendar/firstappで詳細を読む

于 2013-01-25T13:53:58.410 に答える
0

プレーンなJavaScriptを使用して、上記を微調整し、動作させることができました! これは、ユーザーがカレンダーにアクセスして更新するための認証側に主に焦点を当てていることがわかったGoogleドキュメントとして非常に役立ちました. 正確にどこに (どの正確なエンドポイントで) 呼び出しを行う必要があるかを実際に突き止めるのは、より困難でした。明らかに、あなたの calendarId と API キーが必要な場所に XXXXX を配置しました。

    //async function to handle data fetching
    async function getData () {
    //try catch block to handle promises and errors
    try {
        const calendarId = 'XXXXXXXXXXXXX@group.calendar.google.com'
        const myKey = 'XXXXXXXXXXXXXXXXXXXXXXXXXXX'
        //using await and fetch together as two standard ES6 client side features to extract the data
        let apiCall = await fetch('https://www.googleapis.com/calendar/v3/calendars/' + calendarId+ '/events?key=' + myKey)
        //response.json() is a method on the Response object that lets you extract a JSON object from the response
        //response.json() returns a promise resolved to a JSON object
        let apiResponse = await apiCall.json()
        console.log(apiResponse)
    } catch (error) {
        console.log(error)
    }
}
getData()
于 2021-07-07T13:04:35.000 に答える