google adwords apiについて
google adwords apiの使用について
以下ガイドにそってsetup_oauth2.rbを作成し
ruby setup_oauth2.rb
のコマンドを叩いたところ
No such file or directory @ rb_sysopen - /Users/xxxxxxxxx/adwords_api.yml (Errno::ENOENT)
のエラーが発生。
adwords_api.yml
---
# This is an example configuration file for the AdWords API client library.
# Please fill in the required fields, and copy it over to your home directory.
:authentication:
# Authentication method, methods currently supported:
# OAUTH2, OAUTH2_SERVICE_ACCOUNT.
:method: OAuth2
# Auth parameters for OAUTH2 method.
# Set the OAuth2 client id and secret. Register your application here to
# obtain these values:
# https://console.developers.google.com/
:oauth2_client_id: INSERT_OAUTH2_CLIENT_ID_HERE
:oauth2_client_secret: INSERT_OAUTH2_CLIENT_SECRET_HERE
# Optional, see: https://developers.google.com/accounts/docs/OAuth2WebServer
#:oauth2_callback: INSERT_OAUTH2_CALLBACK_URL_HERE
#:oauth2_state: INSERT_OAUTH2_STATE_HERE
#:oauth2_access_type: INSERT_OAUTH2_ACCESS_TYPE_HERE
#:oauth2_prompt: INSERT_OAUTH2_PROMPT_HERE
# You can define extra scopes so that you can reuse your refresh token for
# other APIs.
#:oauth2_extra_scopes: [INSERT_EXTRA_SCOPES_HERE]
# Auth parameters for OAUTH2_SERVICE_ACCOUNT method. See:
# https://developers.google.com/accounts/docs/OAuth2ServiceAccount
# You can provide path to a file with 'oauth2_keyfile' or the key itself with
# 'oauth2_key' option.
#:oauth2_keyfile: INSERT_OAUTH2_KEYFILE_HERE
# Specify the issuer only if you are using a key directly with no key file.
#:oauth2_key: INSERT_OAUTH2_KEY_HERE
#:oauth2_issuer: INSERT_OAUTH2_ISSUER_HERE
# To impersonate a user set prn to an email address.
#:oauth2_prn: INSERT_OAUTH2_PRN_HERE
# Other parameters.
:developer_token: INSERT_YOUR_DEVELOPER_TOKEN_HERE
:client_customer_id: INSERT_YOUR_CLIENT_CUSTOMER_ID_HERE
:user_agent: INSERT_YOUR_USER_AGENT_HERE
:connection:
# Enable to request all responses to be compressed.
:enable_gzip: false
# If your proxy connection requires authentication, make sure to include it in
# the URL, e.g.: http://user:password@proxy_hostname:8080
# :proxy: INSERT_PROXY_HERE
:library:
# Optional: set the log level.
:log_level: INFO
# Optional: uncomment to skip header / summary rows in reporting.
#:skip_report_header: true
#:skip_report_summary: true
#:skip_column_header: true
# Optional: uncomment to disable user agent showing used utilities.
#:include_utilities_in_user_agent: false
setup_oauth2.rb
#!/usr/bin/env ruby
# Encoding: utf-8
#
# Copyright:: Copyright 2013, Google Inc. All Rights Reserved.
#
# License:: Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This example illustrates how to set up OAuth2 authentication credentials.
require 'adwords_api'
def setup_oauth2()
# AdwordsApi::Api will read a config file from ENV['HOME']/adwords_api.yml
# when called without parameters.
adwords = AdwordsApi::Api.new
# To enable logging of SOAP requests, set the log_level value to 'DEBUG' in
# the configuration file or provide your own logger:
# adwords.logger = Logger.new('adwords_xml.log')
# You can call authorize explicitly to obtain the access token. Otherwise, it
# will be invoked automatically on the first API call.
# There are two ways to provide verification code, first one is via the block:
token = adwords.authorize() do |auth_url|
puts "Hit Auth error, please navigate to URL:\n\t%s" % auth_url
print 'log in and type the verification code: '
verification_code = gets.chomp
verification_code
end
if token
print "\nWould you like to update your adwords_api.yml to save " +
"OAuth2 credentials? (y/N): "
response = gets.chomp
if ('y'.casecmp(response) == 0) or ('yes'.casecmp(response) == 0)
adwords.save_oauth2_token(token)
puts 'OAuth2 token is now saved to ~/adwords_api.yml and will be ' +
'automatically used by the library.'
end
end
# Alternatively, you can provide one within the parameters:
# token = adwords.authorize({:oauth2_verification_code => verification_code})
# Note, 'token' is a Hash. Its value is not used in this example. If you need
# to be able to access the API in offline mode, with no user present, you
# should persist it to be used in subsequent invocations like this:
# adwords.authorize({:oauth2_token => token})
# No exception thrown - we are good to make a request.
end
if __FILE__ == $0
API_VERSION = :v201802
begin
setup_oauth2()
# HTTP errors.
rescue AdsCommon::Errors::HttpError => e
puts "HTTP Error: %s" % e
# API errors.
rescue AdwordsApi::Errors::ApiException => e
puts "Message: %s" % e.message
puts 'Errors:'
e.errors.each_with_index do |error, index|
puts "\tError [%d]:" % (index + 1)
error.each do |field, value|
puts "\t\t%s: %s" % [field, value]
end
end
end
end
以下のファイルの構成になっておりましてoauthファイルの
if ('y'.casecmp(response) == 0) or ('yes'.casecmp(response) == 0)
adwords.save_oauth2_token(token)
puts 'OAuth2 token is now saved to ~/adwords_api.yml and will be ' +
'automatically used by the library.'
end
ここの部分に問題があるのかと思いましたが何がご存知の方がいらしゃいましたらご教授頂けますと幸いです。
何か不足などございましたらご指摘いただければと思います。