{"id":7056,"date":"2012-04-10T11:19:38","date_gmt":"2012-04-10T02:19:38","guid":{"rendered":"http:\/\/fukata.org\/?p=7056"},"modified":"2017-08-11T22:58:48","modified_gmt":"2017-08-11T22:58:48","slug":"google-oauth2-use-net-oauth2","status":"publish","type":"post","link":"https:\/\/blog.fukata.org\/archives\/7056\/","title":{"rendered":"Net::OAuth2\u3092\u4f7f\u3063\u3066Google OAuth2\u3092\u3084\u3063\u3066\u307f\u305f"},"content":{"rendered":"
perl\u306eOAuth\u30e2\u30b8\u30e5\u30fc\u30eb\u3067\u3042\u308b\u300cNet::OAuth2<\/a>\u300d\u3092\u4f7f\u3044\u3001Google OAuth2\u3092\u884c\u306a\u3063\u3066\u307f\u305f\u306e\u3067\u30ed\u30b0\u3068\u3057\u3066\u6b8b\u3057\u3066\u304a\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n \u307e\u305a\u3001OAuth\u3067\u4f7f\u7528\u3059\u308b\u30a2\u30d7\u30ea\u306f\u3053\u3053<\/a>\u304b\u3089\u4f5c\u6210\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n \u30bd\u30fc\u30b9\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n <\/p>\n FW\u306fDancer\u3092\u4f7f\u3063\u3066\u3044\u308b\u305f\u3081\u3001\u4e00\u90e8Dancer\u306e\u30b7\u30f3\u30bf\u30c3\u30af\u30b9\u304c\u542b\u307e\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n perl\u306eOAuth\u30e2\u30b8\u30e5\u30fc\u30eb\u3067\u3042\u308b\u300cNet::OAuth2\u300d\u3092\u4f7f\u3044\u3001Google OAuth2\u3092\u884c\u306a\u3063\u3066\u307f\u305f\u306e\u3067\u30ed\u30b0\u3068\u3057\u3066\u6b8b\u3057\u3066\u304a\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002 \u307e\u305a\u3001OAuth\u3067\u4f7f\u7528\u3059\u308b\u30a2\u30d7\u30ea\u306f\u3053\u3053\u304b\u3089\u4f5c\u6210\u3057 … \u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":2,"featured_media":9223372036854775807,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1980],"tags":[969,971,970],"_links":{"self":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts\/7056"}],"collection":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/comments?post=7056"}],"version-history":[{"count":0,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/posts\/7056\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/media\/9223372036854775807"}],"wp:attachment":[{"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/media?parent=7056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/categories?post=7056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.fukata.org\/wp-json\/wp\/v2\/tags?post=7056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}\r\nuse Dancer ':syntax';\r\nuse Net::OAuth2::Client;\r\n \r\nsub oauth {\r\n return Net::OAuth2::Client->new(\r\n \"Client ID\",\r\n \"Client secret\",\r\n site => 'https:\/\/www.googleapis.com',\r\n authorize_url => 'https:\/\/accounts.google.com\/o\/oauth2\/auth',\r\n access_token_url => 'https:\/\/accounts.google.com\/o\/oauth2\/token',\r\n )->web_server( redirect_uri => uri_for('\/auth\/google\/callback') );\r\n}\r\n\r\nget '\/auth\/google' => sub {\r\n my $url = oauth->authorize_url(\r\n scope => 'https:\/\/www.googleapis.com\/auth\/userinfo.profile',\r\n );\r\n redirect $url;\r\n};\r\n\r\nget '\/auth\/google\/callback' => sub {\r\n if ( params->{error} ) {\r\n # OAuth\u62d2\u5426\r\n }\r\n elsif ( params->{code} ) {\r\n # OAuth\u8a31\u53ef\r\n my $access_token = oauth->get_access_token( params->{code} );\r\n my $response = $access_token->get('\/oauth2\/v1\/userinfo');\r\n if ( $response->is_success ) {\r\n my $user = from_json( $response->decoded_content, { utf8 => 1 } );\r\n # \u30e6\u30fc\u30b6\u60c5\u5831\u3092\u4f7f\u3063\u3066\u4f55\u304b\u51e6\u7406...\r\n }\r\n }\r\n};\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"