Diego81 / omnicontacts

A generalized Rack middleware for importing contacts from major email providers.
477 stars 231 forks source link

outlook support missing #159

Closed hugovelias closed 6 years ago

hugovelias commented 8 years ago

Outlook is mentioned in Readme but not actually included in last Version.

SebFalque commented 8 years ago

Hello, I'm although unable to access outlook/hotmail contacts whereas the api seems to be reacheable

dodontommy commented 8 years ago

+1 I'm getting Could not find importer outlook. error message while trying to start my Rails server

georgecoltart commented 8 years ago

Likewise having an issue with Outlook, my rails server won't even start when the outlook option in the config file.

Anyone get this working?

georgecoltart commented 8 years ago

Use the latest 0.3.9 with gem "omnicontacts", github: 'Diego81/omnicontacts' in your gemfile.

AleksandrLeontev commented 8 years ago

Can someone please show outlook settings?

Rails.application.middleware.use OmniContacts::Builder do importer :outlook, Rails.configuration.x.outlook[:app_id], Rails.configuration.x.outlook[:app_secret] end

It is doesn't work for me

in outlook url I see this error:

error=invalid_request&error_description=The provided value for the input parameter 'redirect_uri' is not valid. The expected value is 'https://login.live.com/oauth20_desktop.srf' or a URL which matches the redirect URI registered for this client application.

hugovelias commented 8 years ago

dont remember the exact cause but I did this hack to make it work,

omnicontacts_initializer.rb

Rails.application.middleware.use OmniContacts::Builder do
  importer :outlook, outlook_key, outlook_secret
end

# hack to pull contacts with valid emails from microsoft api.

class OmniContacts::Importer::Outlook < OmniContacts::Importer::Hotmail

  def initialize *args
    super *args
    @auth_host = "login.microsoftonline.com"
    @authorize_path = "/common/oauth2/v2.0/authorize"
    @scope = "https://outlook.office.com/contacts.read"
    @auth_token_path = "/common/oauth2/v2.0/token"
    @contacts_host = "outlook.office.com"
    @contacts_path = "/api/v2.0/me/contacts"
    @self_path = "/api/v2.0/me"
  end

  def fetch_contacts_using_access_token access_token, token_type
    fetch_current_user(access_token, token_type)
    contacts_response = https_get(@contacts_host, @contacts_path, {}, contacts_req_headers(access_token, token_type))
    contacts_from_response contacts_response
  end

  def fetch_current_user access_token, token_type
    self_response = https_get(@contacts_host, @self_path, {}, contacts_req_headers(access_token, token_type))
    user = current_user self_response
    set_current_user user
  end

  def contacts_req_headers token, token_type
    { "Authorization" => "#{token_type} #{token}" }
  end

  def current_user me
    return nil if me.nil?
    me = JSON.parse(me)

    name_splitted = me["DisplayName"].split(" ")
    first_name = name_splitted.first
    last_name = name_splitted.last if name_splitted.size > 1

    user = empty_contact
    user[:id]         = me["Id"]
    user[:email]      = me["EmailAddress"]
    user[:name]       = me["DisplayName"]
    user[:first_name] = normalize_name(first_name)
    user[:last_name]  = normalize_name(last_name)
    user
  end

  def contacts_from_response response_as_json
    response = JSON.parse(response_as_json)
    contacts = []
    response["value"].each do |entry|
      contact = empty_contact
      # Full fields reference:
      # https://msdn.microsoft.com/office/office365/api/complex-types-for-mail-contacts-calendar#RESTAPIResourcesContact
      contact[:id]         = entry["Id"]
      contact[:first_name] = entry["GivenName"]
      contact[:last_name]  = entry["Surname"]
      contact[:name]       = entry["DisplayName"]
      contact[:email]      = parse_email(entry["EmailAddresses"])
      contact[:birthday]   = birthday(entry["Birthday"])

      address = [entry["HomeAddress"], entry["BusinessAddress"], entry["OtherAddress"]].reject(&:empty?).first
      if address
        contact[:address_1] = address["Street"]
        contact[:city]      = address["City"]
        contact[:region]    = address["State"]
        contact[:postcode]  = address["PostalCode"]
        contact[:country]   = address["CountryOrRegion"]
      end

      contacts << contact if contact[:name] || contact[:first_name]
    end
    contacts
  end

  def empty_contact
    { :id => nil, :first_name => nil, :last_name => nil, :name => nil, :email => nil,
      :gender => nil, :birthday => nil, :profile_picture => nil, :address_1 => nil,
      :address_2 => nil, :city => nil, :region => nil, :postcode => nil, :relation => nil }
  end

  def parse_email emails
    return nil if emails.nil?
    emails.map! { |email| email["Address"] }
    emails.select! { |email| valid_email? email }
    emails.first
  end

  def birthday dob
    return nil if dob.nil?
    birthday = dob[0..9].split("-")
    birthday[0] = nil if birthday[0].to_i < 1900 # if year is not set it returns 1604
    return birthday_format(birthday[1], birthday[2], birthday[0])
  end

  def valid_email? value
    /\A[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]+\z/.match(value)
  end`
troex commented 8 years ago

Hey guys I'm contributor of outlook support, as @georgecoltart mentioned use gem 'omnicontacts', github: 'Diego81/omnicontacts' when adding gem. Outlook is only available in git version, the last released version on rubygems doesn't include Outlook support.

@rubytastic can you please publish new version to rubygems?

troex commented 8 years ago

@AleksandrLeontev you need to add "Web" platform to your application on developer portal, Goto your application on Dev portal click "Add Platform" when editing app and put your callback url - http://YOUR_SITE/contacts/outlook/callback if you used default omnicontacts setup.

joevandyk commented 7 years ago

this can be closed right?

AleksandrLeontev commented 7 years ago

@joevandyk Yes, Microsoft Outlook added some scopes for emails, etc... in their web interface - after that your gem works perfectly. So, the problem was on their side. @Diego81 Thank you for your gem.

joevandyk commented 7 years ago

@AleksandrLeontev can you point to where you have to go to allow access to emails? was that hack above necessary?

joevandyk commented 7 years ago

@Diego81 any chance you could publish a new version to rubygems?

AleksandrLeontev commented 7 years ago

@joevandyk You need to go here: https://apps.dev.microsoft.com/#/appList register your applications and add special scopes (emails)

sylvinho81 commented 6 years ago

I am trying to import the outlook contacts, but microsoft is telling me that "there are problems with microsoft right now" or something like that. I tried it several times, but always the same. It can not be a problem with the microsoft credentials, because I can do login in the app through this account.

sylvinho81 commented 6 years ago

Sorry guys , there is a open issue for my question #168